﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Core;
using dtSearch32Lib;
using DocumentSearchServiceModel;
using DocumentSearch32Lib;
using Portal.Systems.Model;
using Portal.Workflow.Bussiness;
using Portal.Cms.Utility;
using System.Data;

namespace Portal.Cms.Render
{
    public class CmsDocumentDataRenderBll
    {
        static string GetPlugInJavascript(string SiteId, string ParentDataId)
        {

            string ScriptText =
                "<input type=hidden id=txtParentDataId value=\"" + ParentDataId + "\">\r\n" +
                   "<script language=javascript>\r\n" +
                   "   function ClientUpdateIndex(GroupDataId)\r\n" +
                   "   {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideIndex(SiteId,GroupDataId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        alert(AjaxOut.InfoMessage);\r\n" +
                   "   }\r\n" +

                   "   function ClientFolderClick(groupDataId,folderId)\r\n" +
                   "   {\r\n" +
                   "        alert(groupDataId + '----- '+ folderId); \r\n" +
                   "        if(folderId != null)\r\n" +
                   "        {\r\n" +
                   "            window.open('AddCmsDocumentData.aspx?gid='+groupDataId +'&GroupDataFolderId='+folderId,'_self');\r\n" +
                   "        }\r\n" +
                   "   }\r\n" +

                   "   function OnActiveNodeClick()\r\n" +
                   "   {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                   "        alert(SelectedNode);\r\n" +
                   "        if(SelectedNode != null)\r\n" +
                   "        {\r\n" +
                   "            alert(SelectedNode);\r\n" +
                   "            GroupDataFolderId=SelectedNode.data.href ;\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "   }\r\n" +

                   "    function ClientAdd(GroupDataId, GroupDataFolderId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                //"        CheckFolder = document.getElementById('txtCheckFolder').value;\r\n"+
                //"        if(CheckFolder=='1')\r\n"+
                //"        {\r\n"+
                //"            var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                //"            if(SelectedNode==null)\r\n" +
                //"            {\r\n" +
                //"                alert('Chưa chọn vào thư mục!');\r\n" +
                //"                return;\r\n" +
                //"            }\r\n" +
                //"            GroupDataFolderId=SelectedNode.data.href;\r\n" +
                //"            if(GroupDataFolderId==null)\r\n" +
                //"            {\r\n" +
                //"                alert('Chọn vào thư mục cụ thể không chọn thư mục gốc!');\r\n" +
                //"                return;\r\n" +
                //"            }\r\n" +
                //"            if(GroupDataFolderId=='root')\r\n"+
                //"            {\r\n" +
                //"                alert('Chọn vào thư mục cụ thể không chọn thư mục gốc!');\r\n" +
                //"                return;\r\n" +
                //"            }\r\n" +
                //"        alert(GroupDataFolderId); \r\n"+
                //"        if(GroupDataFolderId != '#'){\r\n" +
                   "            window.open('AddCmsDocumentData.aspx?gid='+GroupDataId+'&GroupDataFolderId='+GroupDataFolderId,'_self');\r\n" +
                //"        }\r\n"+
                //"        else\r\n"+
                //"        {\r\n"+
                //"                alert('Chọn vào thư mục cụ thể không chọn thư mục gốc!');\r\n" +
                ////"            window.open('AddCmsDocumentData.aspx?gid='+GroupDataId,'_self');\r\n" +
                //"        }\r\n"+
                   "    }\r\n" +

                   "    function ClientChangeAvatar(GroupDataId,CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('txtChangeAvatarUpdateId').value=CmsDataId;\r\n" +
                   "        document.getElementById('divActionChangeAvatarForm').style.display='block';\r\n" +
                   "        document.getElementById('sidebar').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        document.getElementById('txtSubject').focus();\r\n" +
                   "    }\r\n" +

                   "    function ClientMoveGroupDatabaseFolder(GroupDataId,CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        window.open('MoveDocumentToGroupDataFolder.aspx?gid='+GroupDataId+'&DocumentId='+CmsDataId,'_self');\r\n" +
                   "    }\r\n" +

                   "    function ClientAttachDoc(GroupDataId,CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('txtChangeAttachDocUpdateId').value=CmsDataId;\r\n" +
                   "        document.getElementById('divActionAttachDocForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        document.getElementById('sidebar').style.display='none';\r\n" +
                   "        document.getElementById('txtSubject').focus();\r\n" +
                   "    }\r\n" +

                   "    function ClientUpdate(GroupDataId,CmsDataId,UrlUpdate)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   //"        window.open('UpdateCmsDocumentData.aspx?gid='+GroupDataId+'&CmsDataId='+CmsDataId,'_self');\r\n" +
                   "        window.open(UrlUpdate,'_self');\r\n" +
                   "    }\r\n" +


                   "    function ClientListAttachFile(GroupDataId,CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "    }\r\n" +


                   "    function ClientBack()\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('divActionChangeAvatarForm').style.display='none';\r\n" +
                   "        document.getElementById('divActionAttachDocForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "        document.getElementById('sidebar').style.display='block';\r\n" +
                   "    }\r\n" +



                   "    function ClientDelete(DataType,CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                   "        ParentDataId=document.getElementById('txtParentDataId').value;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideDelete(SiteId,DataType, ParentDataId, CmsDataId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        if(AjaxOut.RetUrl!='')\r\n" +
                   "        {\r\n" +
                   "          window.open(AjaxOut.RetUrl,'_self');\r\n" +
                   "          return;\r\n" +
                   "        }\r\n" +
                   "        else\r\n" +
                   "        {\r\n" +
                   "            document.getElementById('divListForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        }\r\n" +
                   "    }\r\n" +

                   "    function ClientPublic(CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn phát hành chưa?')==false)return;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSidePublic(SiteId,CmsDataId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +

                   "        document.getElementById('divStatusText'+CmsDataId).innerHTML = AjaxOut.InfoMessage;\r\n" +
                   "        document.getElementById('divButton'+CmsDataId).innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                   "    function ClientUnPublic(CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn hủy đăng chưa?')==false)return;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideUnPublic(SiteId,CmsDataId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divStatusText'+CmsDataId).innerHTML = AjaxOut.InfoMessage;\r\n" +
                   "        document.getElementById('divButton'+CmsDataId).innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                    "   function ClientDownloadAttachFile(DataId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideDownloadAttachFile(SiteId,DataId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_blank');\r\n" +
                    "   }\r\n" +

                   "    function ClientRelationDocument(GroupDataId,CmsDataId)\r\n" +
                   "    {\r\n" +
                   "    }\r\n" +

                   "    function ProcessHotCategory(Id)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        checked=document.getElementById('chkHot'+Id).checked;\r\n" +
                   "        CheckedIds=document.getElementById('txtAddCheckedHotCategory').value;\r\n" +
                   "        CheckedIds = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,checked).value;\r\n" +
                   "        document.getElementById('txtAddCheckedHotCategory').value=CheckedIds;\r\n" +


                   "        if(checked)\r\n" +
                   "        {\r\n" +
                   "            document.getElementById('chk'+Id).checked=true;\r\n" +

                   "            CheckedIds=document.getElementById('txtAddCheckedCategory').value;\r\n" +
                   "            CheckedIds = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,true).value;\r\n" +
                   "            document.getElementById('txtAddCheckedCategory').value=CheckedIds;\r\n" +


                   "        }\r\n" +
                   "    }\r\n" +

                   "    function ProcessCheckCategory(Id)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        checked=document.getElementById('chk'+Id).checked;\r\n" +
                   "        CheckedIds=document.getElementById('txtAddCheckedCategory').value;\r\n" +
                   "        CheckedIds = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,checked).value;\r\n" +
                   "        document.getElementById('txtAddCheckedCategory').value=CheckedIds;\r\n" +
                   "        if(checked==false)\r\n" +
                   "        {\r\n" +
                   "            document.getElementById('chkHot'+Id).checked=false;\r\n" +


                   "            CheckedIds=document.getElementById('txtAddCheckedHotCategory').value;\r\n" +
                   "            CheckedIds = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,false).value;\r\n" +
                   "            document.getElementById('txtAddCheckedHotCategory').value=CheckedIds;\r\n" +


                   "        }\r\n" +
                   "    }\r\n" +
                   "</script>\r\n";
            return ScriptText;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideProcessCheckCategory(string SiteId, string CheckedIds, string Id, bool Checked)
        {
            if (Checked)
            {
                if (!string.IsNullOrEmpty(CheckedIds))
                {
                    CheckedIds = CheckedIds + ";";
                }
                CheckedIds = CheckedIds + Id;
            }
            else
            {
                CheckedIds = CheckedIds.Replace(Id, "");
            }
            CheckedIds = CheckedIds.Replace(";;", ";");
            if (CheckedIds.Length > 0)
            {
                if (CheckedIds[0].ToString().Equals(";"))
                {
                    CheckedIds = CheckedIds.Substring(1);
                }
            }
            return CheckedIds;
        }




        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                string ParentDataId = (string)WebEnvironments.Request(SiteId, "parent");
                string Html =
                   GetPlugInJavascript(SiteId, ParentDataId) +
                   "<div class=FormFunction>\r\n" +
                   "    <div class=body>\r\n" +
                   "        <div id=sidebar></div>\r\n" +
                    //"        <div id=divActionUpdateForm  style=\"display:none;\">" + DrawUpdateForm(SiteId, GroupDataId).HtmlContent + "</div>\r\n" +
                    //"        <div id=divActionAddForm style=\"display:none;\">" + DrawAddForm(SiteId, GroupDataId).HtmlContent + "</div>\r\n" +
                   "        <div id=divActionChangeAvatarForm  style=\"display:none;\">" + DrawChangeAvatarForm(SiteId, GroupDataId).HtmlContent + "</div>\r\n" +
                   "        <div id=divActionAttachDocForm  style=\"display:none;\">" + DrawChangeAttachDocForm(SiteId, GroupDataId).HtmlContent + "</div>\r\n" +
                   "        <div id=divListForm class=tabcontent>\r\n" +
                                ServerSideReading(SiteId, GroupDataId, ParentDataId).HtmlContent +
                   "        </div>\r\n" +
                   "    </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
        public static string BaseSiteUrl
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
                return baseUrl;
            }
        }

        public static string ConvertUrl(string Url)
        {
            Url = Url.Replace("192.168.1.2/", BaseSiteUrl + "/");
            Url = Url.Replace("117.3.4.207/", BaseSiteUrl + "/");
            return Url;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId, string GroupDataId, string ParentDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu này! Truy cập không hợp lệ!");
                string AppendTitle = "";
                if (string.IsNullOrEmpty(ParentDataId) == false)
                {
                    CmsDataCls SubOCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, ParentDataId);
                    AppendTitle = "<br> " + SubOCmsData.Subject;
                }
                CmsDataCls[] CmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingCmsData(SiteId, GroupDataId, ParentDataId);
               
                string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam);

                string BackUrl = "Home.aspx";
                string Html =
                   "  <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAdd('" + GroupDataId + "');\"><img class=image src=\"" + ButtonUrl + "/Add.png\"><div>Thêm mới</div></li>\r\n" +
                   "            <li onclick=\"" + BackUrl + "\"><img class=image src=\"" + ButtonUrl + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "        <h3><a href=\"InputData.aspx?gid=" + OGroupData.GroupDataId + "\">" + OGroupData.GroupDataName + "</a>" + AppendTitle + " (" + CmsData.Length + ")</h3>\r\n" +
                   "        <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +                    
                   "                    <th class=thSubject>Tiêu đề</th>\r\n" +
                   "                    <th class=thStatus>Trạng thái</th>\r\n" +
                   "                    <th class=thDate>Ngày tạo</th>\r\n" +
                   "                    <th class=thFunc></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < CmsData.Length; iIndex++)
                {
                    string ReleaseButton =
                        "<input type=button class=button value=\"Duyệt\" onclick=\"javascript:ClientPublic('" + CmsData[iIndex].DataId + "');\">";
                    if (CmsData[iIndex].IsPublic == 1)
                    {
                        ReleaseButton =
                            "<input type=button class=button value=\"Hủy đăng\" onclick=\"javascript:ClientUnPublic('" + CmsData[iIndex].DataId + "');\">";
                    }

                    string SubMenuButton = "";
                    if (OGroupData.AllowSub == 1)
                    {
                        SubMenuButton =
                            "<input style=\"width:80px;font-size:12px;\" type=button class=button value=\"Dữ liệu con\" onclick=\"javascript:window.open('InputData.aspx?gid=" + OGroupData.GroupDataId + "&parent=" + CmsData[iIndex].DataId + "','_self');\">";
                    }                    

                    string DisplayInfo = "<span class=NewsSubject><a href=\"PreviewCmsData.aspx?gid=" + CmsData[iIndex].frkGroupDataId + "&CmsDataId=" + CmsData[iIndex].DataId + "\">" + CmsData[iIndex].Subject + "</a></span><br><span class=CssImageInclude>" + (CmsData[iIndex].Avatar.Length > 0 ? "<a href=\"" + CmsData[iIndex].GetAvatarUrl(SiteId, WebConfig.HttpDataRoot(OSiteParam)) + "\" target=\"_blank\">Có ảnh kèm theo</a>" : "") + "</span><br><span class=CssImageInclude>" + (CmsData[iIndex].AttachDoc.Length > 0 ? "<a href=\"" + CmsData[iIndex].GetAttachDocUrl(SiteId, WebConfig.HttpDataRoot(OSiteParam)) + "\" target=\"_blank\">Có tài liệu kèm theo</a>" : "") + "</span><div class=CssItemDesc>" + FunctionUtilities.CutText(CmsData[iIndex].Description, 15) + "</div>\r\n";

                    if (CmsData[iIndex].DataType.Equals("images") || CmsData[iIndex].DataType.Equals("album"))
                    {
                        string ImageUrl = CmsData[iIndex].GetSmallAvatarUrl(SiteId, WebConfig.HttpDataRoot(OSiteParam));
                        if (CmsData[iIndex].HasImage(SiteId, WebConfig.DataPathRoot(OSiteParam)))
                        {
                            ImageUrl = ConvertUrl(ImageUrl);
                            DisplayInfo = "<img class=CssImageThumb src=\"" + ImageUrl + "\">";
                        }
                        else
                        {
                            DisplayInfo = "Chưa có ảnh đại diện";
                        }                    
                    }

                    ///Add new to check per
                    bool hasPer = true;
                    CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, CmsData[iIndex].DataId);
                    if (OGroupData == null) throw new Exception("Tham số truy cập không hợp lệ. Nhóm tin không tìm thấy!");
                    if (OCmsData == null) throw new Exception("Tham số truy cập không hợp lệ. Bài viết không tìm thấy hoặc đã được xóa!");                    

                    bool HasStarted = WorkflowUtilities.CheckHasStartWorkflow(SiteId, CmsData[iIndex].DataId);
                    if (HasStarted == false)
                    {
                        //neu chua nam trong quy trinh thi kiem tra binh thuong
                        SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                    }
                    else
                    {
                        //net nam trong quy trinh
                        string WorkflowDefId = OCmsData.WorkflowDefId;
                        if (!string.IsNullOrEmpty(WorkflowDefId))//co quy trinh
                        {
                            Portal.Workflow.Bussiness.WorkflowBll.GetWorkflowCls
                                  OWorkFlowInfo = WorkflowBll.GetWorkflowActionInfo(OSiteParam.ConnectionString, WorkflowDefId, CmsData[iIndex].DataId);
                            //lay thong tin
                            if (OWorkFlowInfo == null)
                            {
                                //neu khong duoc chuyen thi la chua den luot minh
                                //throw new Exception("Bài viết đang nằm trong quy trình và bạn không có quyền sửa");
                                hasPer = false;
                            }
                            else
                            {
                                if (OWorkFlowInfo.IsViewer)
                                {
                                    //da duoc xu ly
                                    throw new Exception("Bài viết  trong quy trình và đã được người khác lấy về xử lý bạn không có quyền sửa");
                                }
                            }
                        }
                        else
                        {
                            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                        }
                    }
                    ///end check



                    string AttachDocText = "Ảnh";
                    Html +=
                       "                <tr>\r\n" +
                       "                    <td>" + (iIndex + 1) + "</td>\r\n" +
                        //"                    <td style=\"width:100px;\">" + CmsData[iIndex].DataAlias + "</td>\r\n" +
                       "                    <td>\r\n" +
                                DisplayInfo +
                       "                    </td>\r\n" +
                       "                    <td><div id=\"divStatusText" + CmsData[iIndex].DataId + "\">" + (CmsData[iIndex].IsPublic == 1 ? "Đã duyệt" : "Chờ duyệt") + "</div><div id=\"divButton" + CmsData[iIndex].DataId + "\">" + 
                                    ReleaseButton + 
                       "</div></td>\r\n" +
                       "                    <td style=\"width:120px;\">" + CmsData[iIndex].EntryDate.ToString("dd/MM/yyyy HH:mm") + "</td>\r\n" +

                       "                    <td>\r\n";
                    if (hasPer)
                    {
                        string UrlUpdate = Utility.GetUrlUpdate("update", CmsData[iIndex].DataId, OSiteParam);
                        Html +=
                        "                        <input style=\"width:50px;font-size:10px;\" type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + GroupDataId + "','" + CmsData[iIndex].DataId + "','" + UrlUpdate + "');\">\r\n" +
                        "                        <input style=\"width:50px;font-size:10px;\" type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + GroupDataId + "','" + CmsData[iIndex].DataId + "');\">\r\n";
                    }
                        Html+=
                       "                        <input style=\"width:50px;font-size:10px;\" type=button class=button value=\"" + AttachDocText + "\" onclick=\"javascript:ClientChangeAvatar('" + GroupDataId + "','" + CmsData[iIndex].DataId + "');\">\r\n" +
                                SubMenuButton +
                       "                        <input style=\"width:50px;font-size:10px;\" type=button class=button value=\"Tài liệu\" onclick=\"javascript:ClientAttachDoc('" + GroupDataId + "','" + CmsData[iIndex].DataId + "');\">\r\n" +
                       "                    </td>\r\n" +
                       "                </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawChangeAvatarForm(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                     "<script language=javascript>\r\n " +
                     "function fileSelectedChangeAvatar() {\r\n" +
                     "    var file = document.getElementById('fileToUploadChangeAvatar').files[0];\r\n" +
                     "    if (file) {\r\n" +
                     "        var fileSize = 0;\r\n" +
                     "        if (file.size > 1024 * 1024)\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';\r\n" +
                     "        else\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';\r\n" +
                     "        document.getElementById('fileNameChangeAvatar').innerHTML = 'Tên tệp: ' + file.name;\r\n" +
                     "        document.getElementById('fileSizeChangeAvatar').innerHTML = 'Kích cỡ: ' + fileSize;\r\n" +
                     "        document.getElementById('fileTypeChangeAvatar').innerHTML = 'Loại tệp: ' + file.type;\r\n" +
                     "    }\r\n" +
                     "}\r\n" +

                     "function uploadFileChangeAvatar() {\r\n" +
                     "    if(document.getElementById('fileToUploadChangeAvatar').value==''){\r\n" +
                     "       document.getElementById('txtUploadFileChangeAvatar').value='';\r\n" +
                     "       CallSaveAvatar();\r\n" +
                     "       return;\r\n" +
                     "   }\r\n" +
                     "    var fd = new FormData();\r\n" +
                     "    fd.append(\"fileToUpload\", document.getElementById('fileToUploadChangeAvatar').files[0]);\r\n" +
                     "    var xhr = new XMLHttpRequest();\r\n" +
                     "    xhr.upload.addEventListener(\"progress\", uploadProgressChangeAvatar, false);\r\n" +
                     "    xhr.addEventListener(\"load\", uploadCompleteChangeAvatar, false);\r\n" +
                     "    xhr.addEventListener(\"error\", uploadFailedChangeAvatar, false);\r\n" +
                     "    xhr.addEventListener(\"abort\", uploadCanceledChangeAvatar, false);\r\n" +
                     "    xhr.open(\"POST\", \"CmsUploadHandler.ashx?sid=" + SiteId + "&uni=" + WebSessionUtility.GetCurrentLoginUser().AssetCode + "&gid=" + GroupDataId + "\");\r\n" +
                     "    xhr.send(fd);\r\n" +
                     "}\r\n" +

                     "function uploadProgressChangeAvatar(evt) {\r\n" +
                     "    if (evt.lengthComputable) {\r\n" +
                     "    }\r\n" +
                     "    else {\r\n" +
                     "        document.getElementById('progressNumberChangeAvatar').innerHTML = 'Không tính toán được thời gian';\r\n" +
                     "    }\r\n" +
                     "}\r\n" +

                     " function CallSaveAvatar()\r\n" +
                     " {\r\n" +
                     "      document.getElementById('divCallSaveAvatarProcessing').innerHTML='Tiến hành cập nhật và đánh index tìm kiếm...';\r\n" +
                     "      setTimeout('RealCallSaveAvatar()',10);\r\n" +
                     " }\r\n" +

                     " function RealCallSaveAvatar()\r\n" +
                     " {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      DataId = document.getElementById('txtChangeAvatarUpdateId').value;\r\n" +
                     "      uploadFile = document.getElementById('txtUploadFileChangeAvatar').value;\r\n" +
                     "      ParentDataId=document.getElementById('txtParentDataId').value;\r\n" +
                     "      AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideSaveAvatar(SiteId,'" + GroupDataId + "',ParentDataId, DataId, uploadFile).value;\r\n" +
                     "      document.getElementById('divCallSaveAvatarProcessing').innerHTML='';\r\n" +
                     "      if(AjaxOut.Error)\r\n" +
                     "      {\r\n" +
                     "          alert(AjaxOut.InfoMessage);\r\n" +
                     "          return;\r\n" +
                     "      }\r\n" +
                     "      if(AjaxOut.RetUrl!='')\r\n" +
                     "      {\r\n" +
                     "          window.open(AjaxOut.RetUrl,'_self');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          ClientBack();\r\n" +
                     "      }\r\n" +
                     " }\r\n" +


                     "function uploadCompleteChangeAvatar(evt) {\r\n" +
                     "    /* This event is raised when the server send back a response */\r\n" +
                     "    document.getElementById('txtUploadFileChangeAvatar').value=evt.target.responseText;\r\n" +
                     "    //doan nay chay upload vao day...\r\n" +
                     "    CallSaveAvatar();\r\n" +
                     "   }\r\n  " +

                       "function uploadFailedChangeAvatar(evt) {\r\n" +
                     "       alert(\"Lỗi khi tải tệp dữ liệu lên máy chủ.\");\r\n" +
                     "   }\r\n" +

                     "   function uploadCanceledChangeAvatar(evt) {\r\n" +
                     "        alert(\"Người dùng hủy bỏ tải lên máy chủ hoặc mất kết nối với máy chủ.\");\r\n" +
                     "   }\r\n" +
                     "  </script>\r\n" +
                     "  <input type=hidden id=\"txtChangeAvatarUpdateId\" value=\"\">\r\n" +
                       "  <div class=DatabaseFunction>\r\n" +
                       "        <ul>\r\n" +
                       "            <li onclick=\"javascript:uploadFileChangeAvatar();\"><img class=image src=\"" + ButtonUrl + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                       "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonUrl + "/back.png\"><div>Quay lại</div></li>\r\n" +
                       "        </ul>\r\n" +
                       "  </div>\r\n" +
                       "<div class=CssFormSaveUpdate>\r\n" +
                     "  <h3>Thay đổi ảnh đại diện</h3>\r\n" +
                     " <div id=divCallSaveAvatarProcessing class=CssProcessing></div>\r\n" +
                     "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "         <td class=col1>Ảnh đại diện</td>\r\n" +
                   "         <td><input type=hidden id=\"txtUploadFileChangeAvatar\"><input type=\"file\" name=\"fileToUploadChangeAvatar[]\" id=\"fileToUploadChangeAvatar\" onchange=\"fileSelectedChangeAvatar();\" /></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "           <td></td>\r\n" +
                   "           <td>\r\n" +
                   "               <div id=\"progressNumberChangeAvatar\"></div>\r\n" +
                   "               <div id=\"fileNameChangeAvatar\">\r\n" +
                   "               </div>\r\n" +
                   "               <div id=\"fileSizeChangeAvatar\">\r\n" +
                   "               </div>\r\n" +
                   "               <div id=\"fileTypeChangeAvatar\">\r\n" +
                   "               </div>\r\n" +
                   "           </td>\r\n" +
                   "   </tr>\r\n" +
                    //"   <tr>\r\n" +
                    //"            <td class=c1></td>\r\n" +
                    //"            <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:uploadFileChangeAvatar();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                    //"   </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawChangeAttachDocForm(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                string DocCaption = "Tài liệu gắn kèm:";
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam);

                string Html =
                     "<script language=javascript>\r\n " +
                     "function fileSelectedChangeAttachDoc() {\r\n" +
                     "    var file = document.getElementById('fileToUploadChangeAttachDoc').files[0];\r\n" +
                     "    if (file) {\r\n" +
                     "        var fileSize = 0;\r\n" +
                     "        if (file.size > 1024 * 1024)\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';\r\n" +
                     "        else\r\n" +
                     "            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';\r\n" +
                     "        document.getElementById('fileNameChangeAttachDoc').innerHTML = 'Tên tệp: ' + file.name;\r\n" +
                     "        document.getElementById('fileSizeChangeAttachDoc').innerHTML = 'Kích cỡ: ' + fileSize;\r\n" +
                     "        document.getElementById('fileTypeChangeAttachDoc').innerHTML = 'Loại tệp: ' + file.type;\r\n" +
                     "    }\r\n" +
                     "}\r\n" +

                     "function uploadFileChangeAttachDoc() {\r\n" +
                     "    if(document.getElementById('fileToUploadChangeAttachDoc').value==''){\r\n" +
                     "       document.getElementById('txtUploadFileChangeAttachDoc').value='';\r\n" +
                     "       CallSave();\r\n" +
                     "       return;\r\n" +
                     "   }\r\n" +
                     "    DataId = document.getElementById('txtChangeAttachDocUpdateId').value;\r\n" +
                     "    var fd = new FormData();\r\n" +
                     "    fd.append(\"fileToUpload\", document.getElementById('fileToUploadChangeAttachDoc').files[0]);\r\n" +
                     "    var xhr = new XMLHttpRequest();\r\n" +
                     "    xhr.upload.addEventListener(\"progress\", uploadProgressChangeAttachDoc, false);\r\n" +
                     "    xhr.addEventListener(\"load\", uploadCompleteChangeAttachDoc, false);\r\n" +
                     "    xhr.addEventListener(\"error\", uploadFailedChangeAttachDoc, false);\r\n" +
                     "    xhr.addEventListener(\"abort\", uploadCanceledChangeAttachDoc, false);\r\n" +
                     "    xhr.open(\"POST\", \"CmsUploadAttachFileHandler.ashx?DataId=\"+DataId+\"&sid=" + SiteId + "&uni=" + WebSessionUtility.GetCurrentLoginUser().AssetCode + "&gid=" + GroupDataId + "\");\r\n" +
                     "    xhr.send(fd);\r\n" +
                     "}\r\n" +

                     "function uploadProgressChangeAttachDoc(evt) {\r\n" +
                     "    if (evt.lengthComputable) {\r\n" +
                     "    }\r\n" +
                     "    else {\r\n" +
                     "        document.getElementById('progressNumberChangeAttachDoc').innerHTML = 'Không tính toán được thời gian';\r\n" +
                     "    }\r\n" +
                     "}\r\n" +

                     " function CallSave()\r\n" +
                     " {\r\n" +
                     "      document.getElementById('divCallSaveProcessing').innerHTML='Tiến hành cập nhật và đánh index tìm kiếm...';\r\n" +
                     "      setTimeout('RealCallSave()',10);\r\n" +
                     " }\r\n" +

                     " function RealCallSave()\r\n" +
                     " {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      DataId = document.getElementById('txtChangeAttachDocUpdateId').value;\r\n" +
                     "      uploadFile = document.getElementById('txtUploadFileChangeAttachDoc').value;\r\n" +
                     "      ParentDataId=document.getElementById('txtParentDataId').value;\r\n" +
                     "      AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideSaveAttachDoc(SiteId,'" + GroupDataId + "',ParentDataId, DataId, uploadFile).value;\r\n" +
                     "      document.getElementById('divCallSaveProcessing').innerHTML='';\r\n" +
                     "      if(AjaxOut.Error)\r\n" +
                     "      {\r\n" +
                     "          alert(AjaxOut.InfoMessage);\r\n" +
                     "          return;\r\n" +
                     "      }\r\n" +
                     "      if(AjaxOut.RetUrl!='')\r\n" +
                     "      {\r\n" +
                     "          window.open(AjaxOut.RetUrl,'_self');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          ClientBack();\r\n" +
                     "      }\r\n" +
                     " }\r\n" +


                     "function uploadCompleteChangeAttachDoc(evt) {\r\n" +
                     "    /* This event is raised when the server send back a response */\r\n" +
                     "    document.getElementById('txtUploadFileChangeAttachDoc').value=evt.target.responseText;\r\n" +
                     "    //doan nay chay upload vao day...\r\n" +
                     "    CallSave();\r\n" +
                     "   }\r\n  " +

                       "function uploadFailedChangeAttachDoc(evt) {\r\n" +
                     "       alert(\"Lỗi khi tải tệp dữ liệu lên máy chủ.\");\r\n" +
                     "   }\r\n" +

                     "   function uploadCanceledChangeAttachDoc(evt) {\r\n" +
                     "        alert(\"Người dùng hủy bỏ tải lên máy chủ hoặc mất kết nối với máy chủ.\");\r\n" +
                     "   }\r\n" +
                     "  </script>\r\n" +
                     "  <input type=hidden id=\"txtChangeAttachDocUpdateId\" value=\"\">\r\n" +
                       "  <div class=DatabaseFunction>\r\n" +
                       "        <ul>\r\n" +
                       "            <li onclick=\"javascript:uploadFileChangeAttachDoc();\"><img class=image src=\"" + ButtonUrl + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                       "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonUrl + "/back.png\"><div>Quay lại</div></li>\r\n" +
                       "        </ul>\r\n" +
                       "  </div>\r\n" +

                       "<div class=CssFormSaveUpdate>\r\n" +
                     "  <h3>Thay đổi -  " + DocCaption + "</h3>\r\n" +
                     "<div id=divCallSaveProcessing class=\"CssProcessing\"></div>\r\n" +
                     "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "         <td class=col1>" + DocCaption + "</td>\r\n" +
                   "         <td><input type=hidden id=\"txtUploadFileChangeAttachDoc\"><input type=\"file\" name=\"fileToUploadChangeAttachDoc[]\" id=\"fileToUploadChangeAttachDoc\" onchange=\"fileSelectedChangeAttachDoc();\" /></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "           <td></td>\r\n" +
                   "           <td>\r\n" +
                   "               <div id=\"progressNumberChangeAttachDoc\"></div>\r\n" +
                   "               <div id=\"fileNameChangeAttachDoc\">\r\n" +
                   "               </div>\r\n" +
                   "               <div id=\"fileSizeChangeAttachDoc\">\r\n" +
                   "               </div>\r\n" +
                   "               <div id=\"fileTypeChangeAttachDoc\">\r\n" +
                   "               </div>\r\n" +
                   "           </td>\r\n" +
                   "   </tr>\r\n" +
                    //"   <tr>\r\n" +
                    //"            <td class=c1></td>\r\n" +
                    //"            <td class=c2><input type=button class=button value=\"Chấp nhận\" onclick=\"javascript:uploadFileChangeAttachDoc();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\"></td>\r\n" +
                    //"   </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(string SiteId, string GroupDataId, string ParentDataId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                GroupDataCls OGroupDataCls = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).DeleteCmsData(SiteId, DataId);
                if (OGroupDataCls.frkDataTypeId.Equals("document"))
                {
                    OAjaxOut.RetUrl = "DocumentInputData.aspx?gid=" + GroupDataId;
                }
                else
                {
                    OAjaxOut.HtmlContent = ServerSideReading(SiteId, GroupDataId, ParentDataId).HtmlContent;
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideCreateDataModel(string SiteId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DataId);
                OAjaxOut.RetObject = OCmsData;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSaveAvatar(string SiteId, string GroupDataId, string ParentDataId, string DataId, string uploadFile)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ChangeAvatarFile(SiteId, DataId, uploadFile);
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                if (OGroupData.frkDataTypeId.Equals("document"))
                {
                    OAjaxOut.RetUrl = "DocumentInputData.aspx?gid=" + GroupDataId;
                }
                else
                {
                    OAjaxOut.HtmlContent = ServerSideReading(SiteId, GroupDataId, ParentDataId).HtmlContent;
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSaveAttachDoc(string SiteId, string GroupDataId, string ParentDataId, string DataId, string uploadFile)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ChangeAttachDocFile(SiteId, DataId, uploadFile);
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                if (OGroupData.frkDataTypeId.Equals("document"))
                {
                    OAjaxOut.RetUrl = "DocumentInputData.aspx?gid=" + GroupDataId;
                }
                else
                {
                    OAjaxOut.HtmlContent = ServerSideReading(SiteId, GroupDataId, ParentDataId).HtmlContent;
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSidePublic(string SiteId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).PublicData(null, SiteId, DataId);

                OAjaxOut.HtmlContent =
                    "<input type=button class=button  value=\"Hủy đăng\" onclick=\"javascript:ClientUnPublic('" + DataId + "');\">";
                OAjaxOut.InfoMessage = "Đã duyệt";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideUnPublic(string SiteId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).UnPublicData(null, SiteId, DataId);

                OAjaxOut.HtmlContent =
                    "<input type=button class=button value=\"Duyệt\" onclick=\"javascript:ClientPublic('" + DataId + "');\">";
                OAjaxOut.InfoMessage = "Chờ duyệt";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        public static AjaxOut RenderSearchDocument(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);

                ORenderInfo.JumpPageAspx = "DocumentInputData.aspx";

                string documentno = (string)WebEnvironments.Request(SiteId, "documentno");
                if (documentno == null) documentno = "";

                string Subject = (string)WebEnvironments.Request(SiteId, "Subject");
                if (Subject == null) Subject = "";

                //string sfd = (string)WebEnvironments.Request(SiteId, "sfd");
                //if (sfd == null) sfd = "";

                //string std = (string)WebEnvironments.Request(SiteId, "std");
                //if (std == null) std = "";

                string rfd = (string)WebEnvironments.Request(SiteId, "rfd");
                if (rfd == null) rfd = "";

                string rtd = (string)WebEnvironments.Request(SiteId, "rtd");
                if (rtd == null) rtd = "";

                string efd = (string)WebEnvironments.Request(SiteId, "efd");
                if (efd == null) efd = "";

                string etd = (string)WebEnvironments.Request(SiteId, "etd");
                if (etd == null) etd = "";


                string eefd = (string)WebEnvironments.Request(SiteId, "eefd");
                if (eefd == null) eefd = "";

                string eetd = (string)WebEnvironments.Request(SiteId, "eetd");
                if (eetd == null) eetd = "";

                string ip = (string)WebEnvironments.Request(SiteId, "ip");
                if (ip == null) ip = "";

                string sab = (string)WebEnvironments.Request(SiteId, "sab");
                if (sab == null) sab = "";

                string releasefrom = (string)WebEnvironments.Request(SiteId, "releasefrom");
                if (releasefrom == null) releasefrom = "";

                string docfield = (string)WebEnvironments.Request(SiteId, "docfield");
                if (docfield == null) docfield = "";

                string doctype = (string)WebEnvironments.Request(SiteId, "doctype");
                if (doctype == null) doctype = "";

                string docsigner = (string)WebEnvironments.Request(SiteId, "signer");
                if (docsigner == null) docsigner = "";

                string fulltextsearch = (string)WebEnvironments.Request(SiteId, "fulltextsearch");
                if (string.IsNullOrEmpty(fulltextsearch)) fulltextsearch = "0";

                string fullTextSearchKeyword = (string)WebEnvironments.Request(SiteId, "fullTextSearchKeyword");
                if (string.IsNullOrEmpty(fullTextSearchKeyword)) fullTextSearchKeyword = "";

                string gdf = (string)WebEnvironments.Request(SiteId, "gdf");
                if (gdf == null) gdf = "";
                if (gdf.Equals("root")) gdf = "";

                DocReleaseFromCls[] DocReleaseFroms = DocReleaseFromProcessBll.ReadingDocReleaseFrom(SiteId, OGroupData.GroupDataId);
                string SelectReleaseFrom =
                    " <select class=\"drpselect\" id=\"drpDocumentReleasefrom\">\r\n" +
                    "   <option value=\"\">Toàn bộ</option>\r\n";
                for (int iIndex = 0; iIndex < DocReleaseFroms.Length; iIndex++)
                {
                    if (releasefrom.Equals(DocReleaseFroms[iIndex].DocReleaseFromId))
                    {
                        SelectReleaseFrom += "<option selected value=\"" + DocReleaseFroms[iIndex].DocReleaseFromId + "\">" + DocReleaseFroms[iIndex].DocReleaseFromName + "</option>\r\n";
                    }
                    else
                    {
                        SelectReleaseFrom += "<option value=\"" + DocReleaseFroms[iIndex].DocReleaseFromId + "\">" + DocReleaseFroms[iIndex].DocReleaseFromName + "</option>\r\n";
                    }
                }

                SelectReleaseFrom +=
                    " </select>\r\n";

                DocSignerCls[] DocSigners = DocSignerProcessBll.ReadingDocSigner(SiteId, OGroupData.GroupDataId);
                string SelectSigner =
                    " <select class=\"drpselect\" id=\"drpDocumentSigner\">\r\n" +
                    "   <option value=\"\">Toàn bộ</option>\r\n";
                for (int iIndex = 0; iIndex < DocSigners.Length; iIndex++)
                {
                    if (docsigner.Equals(DocSigners[iIndex].DocSignerId))
                    {
                        SelectSigner += "    <option selected value=\"" + DocSigners[iIndex].DocSignerId + "\">" + DocSigners[iIndex].DocSignerName + "</option>\r\n";
                    }
                    else
                    {
                        SelectSigner += "    <option value=\"" + DocSigners[iIndex].DocSignerId + "\">" + DocSigners[iIndex].DocSignerName + "</option>\r\n";
                    }
                }
                SelectSigner +=
                    " </select>\r\n";

                string SelectFullText =
                     " <select id=drpSelectFullText class=\"drpselect\" >\r\n" +
                     "  <option value=\"0\">Không tìm toàn văn</option>\r\n" +
                     "  <option value=\"1\">Tìm toàn văn</option>\r\n" +
                     " </select>\r\n";

                if (fulltextsearch.Equals("1"))
                {
                    SelectFullText =
                     " <select id=drpSelectFullText class=\"drpselect\" >\r\n" +
                     "  <option value=\"0\">Không tìm toàn văn</option>\r\n" +
                     "  <option selected value=\"1\">Tìm toàn văn</option>\r\n" +
                     " </select>\r\n";
                }

                DocTypeCls[] DocTypes = DocTypeProcessBll.ReadingDocType(SiteId, OGroupData.GroupDataId);
                string SelectDocType =
                    " <select class=\"drpselect\" id=\"drpDocumentDocType\">\r\n" +
                    "   <option value=\"\">Toàn bộ</option>\r\n";
                for (int iIndex = 0; iIndex < DocTypes.Length; iIndex++)
                {
                    if (doctype.Equals(DocTypes[iIndex].DocTypeId))
                    {
                        SelectDocType += "    <option selected value=\"" + DocTypes[iIndex].DocTypeId + "\">" + DocTypes[iIndex].DocTypeName + "</option>\r\n";
                    }
                    else
                    {
                        SelectDocType += "    <option value=\"" + DocTypes[iIndex].DocTypeId + "\">" + DocTypes[iIndex].DocTypeName + "</option>\r\n";
                    }
                }
                SelectDocType +=
                    " </select>\r\n";


                DocFieldCls[] DocFields = DocFieldProcessBll.ReadingDocField(SiteId, OGroupData.GroupDataId);
                string SelectDocField =
                    " <select class=\"drpselect\" id=\"drpDocumentDocField\">\r\n" +
                    "   <option value=\"\">Toàn bộ</option>\r\n";
                for (int iIndex = 0; iIndex < DocFields.Length; iIndex++)
                {
                    if (docfield.Equals(DocFields[iIndex].DocFieldId))
                    {
                        SelectDocField += "    <option selected value=\"" + DocFields[iIndex].DocFieldId + "\">" + DocFields[iIndex].DocFieldName + "</option>\r\n";
                    }
                    else
                    {
                        SelectDocField += "    <option value=\"" + DocFields[iIndex].DocFieldId + "\">" + DocFields[iIndex].DocFieldName + "</option>\r\n";
                    }
                }
                SelectDocField +=
                    " </select>\r\n";

                object DisplayDiv = WebSessionUtility.GetSession(SiteId, "divSearchForm");
                if (DisplayDiv == null)
                {
                    DisplayDiv = "none";
                }
                else
                {
                    if ((bool)DisplayDiv)
                    {
                        DisplayDiv = "block";
                    }
                    else
                    {
                        DisplayDiv = "none";
                    }
                }

                string SelectPublishing =
                    "<select class=\"drpselect\" id=drpSelectPublishing>\r\n" +
                    "   <option value=\"2\">Toàn bộ</option>\r\n" +
                    "   <option value=\"0\">Chờ duyệt</option>\r\n" +
                    "   <option value=\"1\">Đã duyệt</option>\r\n" +
                    "</select>\r\n";

                if (ip.Equals("0"))
                {
                    SelectPublishing =
                    "<select class=\"drpselect\" id=drpSelectPublishing>\r\n" +
                    "   <option value=\"2\">Toàn bộ</option>\r\n" +
                    "   <option selected value=\"0\">Chờ duyệt</option>\r\n" +
                    "   <option value=\"1\">Đã duyệt</option>\r\n" +
                    "</select>\r\n";
                }
                if (ip.Equals("1"))
                {
                    SelectPublishing =
                    "<select class=\"drpselect\" id=drpSelectPublishing>\r\n" +
                    "   <option value=\"2\">Toàn bộ</option>\r\n" +
                    "   <option value=\"0\">Chờ duyệt</option>\r\n" +
                    "   <option selected value=\"1\">Đã duyệt</option>\r\n" +
                    "</select>\r\n";
                }



                string SelectSearchable =
                   "<select class=\"drpselect\" id=drpSelectSearchableForSearch>\r\n" +
                   "   <option value=\"\">Toàn bộ</option>\r\n" +
                   "   <option value=\"0\">Không tìm toàn văn</option>\r\n" +
                   "   <option value=\"1\">Tìm toàn văn</option>\r\n" +
                   "</select>\r\n";

                if (sab.Equals("0"))
                {
                    SelectSearchable =
                    "<select class=\"drpselect\" id=drpSelectSearchableForSearch>\r\n" +
                    "   <option value=\"\">Toàn bộ</option>\r\n" +
                    "   <option selected value=\"0\">Không tìm toàn văn</option>\r\n" +
                    "   <option value=\"1\">Tìm toàn văn</option>\r\n" +
                    "</select>\r\n";
                }
                if (sab.Equals("1"))
                {
                    SelectSearchable =
                    "<select class=\"drpselect\" id=drpSelectSearchableForSearch>\r\n" +
                    "   <option value=\"\">Toàn bộ</option>\r\n" +
                    "   <option value=\"0\">Không tìm toàn văn</option>\r\n" +
                    "   <option selected value=\"1\">Tìm toàn văn</option>\r\n" +
                    "</select>\r\n";
                }
                string Html =
                    " <script language=javascript>\r\n" +
                     "  function CollapseExpandSearchForm(Id)\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      if(document.getElementById(Id).style.display=='block')\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='none';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'false');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='block';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'true');\r\n" +
                     "      }\r\n" +
                     "  }\r\n" +


                    "   function ClientDoSearchDocument(PageIndex)\r\n" +
                    "   {\r\n" +
                    "       DocumentNo=document.getElementById('txtDocumentNo').value;\r\n" +
                    "       Subject=document.getElementById('txtDocumentSubject').value;\r\n" +

                    "       SignFromDate='';//document.getElementById('txtSignFromDate').value;\r\n" +
                    "       SignToDate ='';//document.getElementById('txtSignToDate').value;\r\n" +

                    "       ReleaseFromDate=document.getElementById('txtReleaseFromDate').value;\r\n" +
                    "       ReleaseToDate =document.getElementById('txtReleaseToDate').value;\r\n" +

                    "       EffectedFromDate=document.getElementById('txtEffectedFromDate').value;\r\n" +
                    "       EffectedToDate=document.getElementById('txtEffectedToDate').value;\r\n" +

                    //"       EndEffectedFromDate=document.getElementById('txtEndEffectedFromDate').value;\r\n" +
                    //"       EndEffectedToDate=document.getElementById('txtEndEffectedToDate').value;\r\n" +

                    "       EndEffectedFromDate='';\r\n" +
                    "       EndEffectedToDate='';\r\n" +

                    "       DocumentReleasefrom=document.getElementById('drpDocumentReleasefrom').value;\r\n" +
                    "       DocumentDocType=document.getElementById('drpDocumentDocType').value;\r\n" +
                    "       DocumentDocField=document.getElementById('drpDocumentDocField').value;\r\n" +
                    "       DocumentSigner=document.getElementById('drpDocumentSigner').value;\r\n" +
                    "       SortField=document.getElementById('drpSelectSortField').value;\r\n" +
                    "       SortType=document.getElementById('drpSortType').value;\r\n" +
                    "       PageSize = document.getElementById('drpSelectPageSize').value;\r\n" +
                    "       fulltextsearch = document.getElementById('drpSelectFullText').value;\r\n" +
                    "       fullTextSearchKeyword = document.getElementById('txtFullTextSearch').value;\r\n" +
                    "       ip = document.getElementById('drpSelectPublishing').value;\r\n" +
                   
                    "       sab = document.getElementById('drpSelectSearchableForSearch').value;\r\n" +
                    "       ps = document.getElementById('drpSelectStatus').value;\r\n" +

                    //"       CheckFolder = document.getElementById('txtCheckFolder').value;\r\n" +
                    "       gdf='';\r\n" +
                    //"       if(CheckFolder=='1')\r\n" +
                    //"       {\r\n" +
                    //"           var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                    //"           if(SelectedNode!=null)\r\n" +
                    //"           {\r\n" +
                    //"               gdf=SelectedNode.data.href;\r\n" +
                    //"               if(gdf==null)\r\n" +
                    //"               {\r\n" +
                    //"                   gdf='';\r\n"+
                    //"               }\r\n" +
                    //"           }\r\n"+
                    //"       }\r\n" +

                    //"       window.open('" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&documentno='+DocumentNo+'&subject='+Subject+'&sfd='+SignFromDate+'&std='+SignToDate+'&rfd='+ReleaseFromDate+'&rtd='+ReleaseToDate +'&efd='+EffectedFromDate+'&etd='+EffectedToDate+'&eefd='+EndEffectedFromDate+'&eetd='+EndEffectedToDate+'&releasefrom='+DocumentReleasefrom+'&signer='+DocumentSigner+'&doctype='+DocumentDocType+'&docfield='+DocumentDocField+'&SortField='+SortField+'&SortType='+SortType+'&PageSize='+PageSize+'&PageIndex='+PageIndex+'&fulltextsearch='+fulltextsearch+'&fullTextSearchKeyword='+fullTextSearchKeyword+'&ip='+ip+'&sab='+sab+'&ps='+ps+'&gdf='+gdf,'_self');\r\n" +
                    "       window.open('" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&documentno='+DocumentNo+'&subject='+Subject+'&rfd='+ReleaseFromDate+'&rtd='+ReleaseToDate +'&efd='+EffectedFromDate+'&etd='+EffectedToDate+'&eefd='+EndEffectedFromDate+'&eetd='+EndEffectedToDate+'&releasefrom='+DocumentReleasefrom+'&signer='+DocumentSigner+'&doctype='+DocumentDocType+'&docfield='+DocumentDocField+'&SortField='+SortField+'&SortType='+SortType+'&PageSize='+PageSize+'&PageIndex='+PageIndex+'&fulltextsearch='+fulltextsearch+'&fullTextSearchKeyword='+fullTextSearchKeyword+'&ip='+ip+'&sab='+sab+'&ps='+ps+'&gdf='+gdf,'_self');\r\n" +
                    "   }\r\n" +
                    " </script> \r\n" +
                    " <h3><a href=\"javascript:CollapseExpandSearchForm('divSearchForm');\">Tìm kiếm nâng cao</a></h3> \r\n" +
                    " <div id=\"divSearchForm\" style=\"display:" + DisplayDiv + ";\">\r\n" +

                    " <div>Trạng thái:</div> \r\n" +
                    " <div>" + SelectPublishing + "</div> \r\n" +

                    " <div>Toàn văn:</div> \r\n" +
                    " <div>" + SelectSearchable + "</div> \r\n" +

                    " <div>Số văn bản:</div> \r\n" +
                    " <div><input type=textbox class=textbox id=txtDocumentNo value=\"" + documentno + "\" /></div> \r\n" +
                    " <div>Tiêu đề / mô tả:</div> \r\n" +
                    " <div><input type=textbox class=textbox id=\"txtDocumentSubject\" value=\"" + Subject + "\" /></div> \r\n" +
                    //" <div>Ngày ký:</div> \r\n" +
                    //" <div>" + LookUpUtilities.GenerateLookUpDate("txtSignFromDate", sfd) + "~" + LookUpUtilities.GenerateLookUpDate("txtSignToDate", std) + "</div> \r\n" +
                    " <div>Ngày ban hành:</div> \r\n" +
                    " <div>" + LookUpUtilities.GenerateLookUpDate("txtReleaseFromDate", rfd) + "~" + LookUpUtilities.GenerateLookUpDate("txtReleaseToDate", rtd) + "</div> \r\n" +
                    " <div>Ngày hiệu lực:</div> \r\n" +
                    " <div>" + LookUpUtilities.GenerateLookUpDate("txtEffectedFromDate", efd) + "~" + LookUpUtilities.GenerateLookUpDate("txtEffectedToDate", etd) + "</div> \r\n" +

                    //" <div>Ngày hết hiệu lực:</div> \r\n" +
                    //" <div>" + LookUpUtilities.GenerateLookUpDate("txtEndEffectedFromDate", eefd) + "~" + LookUpUtilities.GenerateLookUpDate("txtEndEffectedToDate", eetd) + "</div> \r\n" +

                    " <div>Loại văn bản:</div> \r\n" +
                    " <div>" + SelectDocType + "</div> \r\n" +

                    " <div>Lĩnh vực:</div> \r\n" +
                    " <div>" + SelectDocField + "</div> \r\n" +

                    " <div>Cơ quan ban hành:</div> \r\n" +
                    " <div>" + SelectReleaseFrom + "</div> \r\n" +
                    " <div>Người ký:</div> \r\n" +
                    " <div>" + SelectSigner + "</div> \r\n" +                  

                    " <div>Toàn văn:</div> \r\n" +
                    " <div>" + SelectFullText + "</div> \r\n" +
                    " <div>Từ khóa toàn văn:</div> \r\n" +
                    " <div><input type=textbox class=textbox id=\"txtFullTextSearch\" value=\"" + fullTextSearchKeyword + "\" /></div> \r\n" +

                    " <div class=search><input type=button class=button value=\"Tìm kiếm văn bản\" onclick=\"javascript:ClientDoSearchDocument(0);\"></div> \r\n" +
                    " </div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        /// <summary>
        /// Loai van ban
        /// </summary>
        /// <param name="SiteId"></param>
        /// <param name="ORenderInfo"></param>
        /// <returns></returns>
        public static AjaxOut RenderDocumentTypeFilter(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);
                string doctype = (string)WebEnvironments.Request(SiteId, "doctype");
                if (doctype == null) doctype = "";
                string gdf = (string)WebEnvironments.Request(SiteId, "gdf");
                if (string.IsNullOrEmpty(gdf)) gdf = "";

                object DisplayDiv = WebSessionUtility.GetSession(SiteId, "ulFilterDocumentType");
                if (DisplayDiv == null)
                {
                    DisplayDiv = "block";
                }
                else
                {
                    if ((bool)DisplayDiv)
                    {
                        DisplayDiv = "block";
                    }
                    else
                    {
                        DisplayDiv = "none";
                    }
                }

                DocTypeCls[] DocTypes = DocTypeProcessBll.ReadingDocType(SiteId, OGroupData.GroupDataId);
                string Html =

                     "<script language=javascript>\r\n" +
                     "  function CollapseExpandDocumentType(Id)\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      if(document.getElementById(Id).style.display=='block')\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='none';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'false');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='block';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'true');\r\n" +
                     "      }\r\n" +
                     "  }\r\n" +
                     "</script>\r\n" +

                     "<h3><a href=\"javascript:CollapseExpandDocumentType('ulFilterDocumentType');\">Loại văn bản</a></h3>\r\n" +
                     "<ul class=ulFilter id=ulFilterDocumentType style=\"display:" + DisplayDiv + "\">\r\n";

                ORenderInfo.JumpPageAspx = "DocumentInputData.aspx";
                for (int iIndex = 0; iIndex < DocTypes.Length; iIndex++)
                {
                    if (doctype.Equals(DocTypes[iIndex].DocTypeId))
                    {
                        Html += "   <li class=active><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&doctype=" + DocTypes[iIndex].DocTypeId + "&gdf=" + gdf + "\">" + DocTypes[iIndex].DocTypeName + "</a></li>\r\n";
                    }
                    else
                    {
                        Html += "   <li><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&doctype=" + DocTypes[iIndex].DocTypeId + "&gdf=" + gdf + "\">" + DocTypes[iIndex].DocTypeName + "</a></li>\r\n";
                    }
                }
                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        /// <summary>
        /// Nguoi ky van ban
        /// </summary>
        /// <param name="SiteId"></param>
        /// <param name="ORenderInfo"></param>
        /// <returns></returns>
        public static AjaxOut RenderSignerFilter(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);
                string Signer = (string)WebEnvironments.Request(SiteId, "Signer");
                if (Signer == null) Signer = "";
                string gdf = (string)WebEnvironments.Request(SiteId, "gdf");
                if (string.IsNullOrEmpty(gdf)) gdf = "";

                object DisplayDiv = WebSessionUtility.GetSession(SiteId, "ulFilterSigner");
                if (DisplayDiv == null)
                {
                    DisplayDiv = "none";
                }
                else
                {
                    if ((bool)DisplayDiv)
                    {
                        DisplayDiv = "block";
                    }
                    else
                    {
                        DisplayDiv = "none";
                    }
                }

                DocSignerCls[] Signers = DocSignerProcessBll.ReadingDocSigner(SiteId, OGroupData.GroupDataId);
                string Html =
                     "<script language=javascript>\r\n" +
                     "  function CollapseExpandSigner(Id)\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      if(document.getElementById(Id).style.display=='block')\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='none';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'false');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='block';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'true');\r\n" +
                     "      }\r\n" +
                     "  }\r\n" +
                     "</script>\r\n" +
                     "<h3><a href=\"javascript:CollapseExpandSigner('ulFilterSigner');\">Người ký</a></h3>\r\n" +
                     "<ul class=ulFilter id=ulFilterSigner style=\"display:" + DisplayDiv + "\">\r\n";
                for (int iIndex = 0; iIndex < Signers.Length; iIndex++)
                {
                    if (Signer.Equals(Signers[iIndex].DocSignerId))
                    {
                        Html += "   <li class=active><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&signer=" + Signers[iIndex].DocSignerId + "&gdf=" + gdf + "\">" + Signers[iIndex].DocSignerName + "</a></li>\r\n";
                    }
                    else
                    {
                        Html += "   <li><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&signer=" + Signers[iIndex].DocSignerId + "&gdf=" + gdf + "\">" + Signers[iIndex].DocSignerName + "</a></li>\r\n";
                    }
                }
                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        /// <summary>
        /// Co quan ban hanh
        /// </summary>
        /// <param name="SiteId"></param>
        /// <param name="ORenderInfo"></param>
        /// <returns></returns>
        public static AjaxOut RenderDocReleaseFromFilter(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);
                string ReleaseFrom = (string)WebEnvironments.Request(SiteId, "ReleaseFrom");
                if (ReleaseFrom == null) ReleaseFrom = "";

                string gdf = (string)WebEnvironments.Request(SiteId, "gdf");
                if (string.IsNullOrEmpty(gdf)) gdf = "";


                object DisplayDiv = WebSessionUtility.GetSession(SiteId, "ulFilterReleaseFrom");
                if (DisplayDiv == null)
                {
                    DisplayDiv = "none";
                }
                else
                {
                    if ((bool)DisplayDiv)
                    {
                        DisplayDiv = "block";
                    }
                    else
                    {
                        DisplayDiv = "none";
                    }
                }

                DocReleaseFromCls[] ReleaseFroms = DocReleaseFromProcessBll.ReadingDocReleaseFrom(SiteId, OGroupData.GroupDataId);
                string Html =
                     "<script language=javascript>\r\n" +
                     "  function CollapseExpandReleaseFrom(Id)\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      if(document.getElementById(Id).style.display=='block')\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='none';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'false');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='block';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'true');\r\n" +
                     "      }\r\n" +
                     "  }\r\n" +
                     "</script>\r\n" +
                     "<h3><a href=\"javascript:CollapseExpandReleaseFrom('ulFilterReleaseFrom');\">Cơ quan ban hành</a></h3>\r\n" +
                     "<ul class=ulFilter id=ulFilterReleaseFrom style=\"display:" + DisplayDiv + "\">\r\n";
                for (int iIndex = 0; iIndex < ReleaseFroms.Length; iIndex++)
                {
                    if (ReleaseFrom.Equals(ReleaseFroms[iIndex].DocReleaseFromId))
                    {
                        Html += "   <li class=active><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&ReleaseFrom=" + ReleaseFroms[iIndex].DocReleaseFromId + "&gdf=" + gdf + "\">" + ReleaseFroms[iIndex].DocReleaseFromName + "</a></li>\r\n";
                    }
                    else
                    {
                        Html += "   <li><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&ReleaseFrom=" + ReleaseFroms[iIndex].DocReleaseFromId + "&gdf=" + gdf + "\">" + ReleaseFroms[iIndex].DocReleaseFromName + "</a></li>\r\n";
                    }
                }
                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        /// <summary>
        /// Linh vuc van ban
        /// </summary>
        /// <param name="SiteId"></param>
        /// <param name="ORenderInfo"></param>
        /// <returns></returns>
        public static AjaxOut RenderDocFieldFilter(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);
                string DocField = (string)WebEnvironments.Request(SiteId, "DocField");
                if (DocField == null) DocField = "";

                string gdf = (string)WebEnvironments.Request(SiteId, "gdf");
                if (string.IsNullOrEmpty(gdf)) gdf = "";


                object DisplayDiv = WebSessionUtility.GetSession(SiteId, "ulFilterDocField");
                if (DisplayDiv == null)
                {
                    DisplayDiv = "none";
                }
                else
                {
                    if ((bool)DisplayDiv)
                    {
                        DisplayDiv = "block";
                    }
                    else
                    {
                        DisplayDiv = "none";
                    }
                }

                DocFieldCls[] DocFields = DocFieldProcessBll.ReadingDocField(SiteId, OGroupData.GroupDataId);
                string Html =
                     "<script language=javascript>\r\n" +
                     "  function CollapseExpandDocField(Id)\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      if(document.getElementById(Id).style.display=='block')\r\n" +
                     "      {\r\n" +
                     "          SiteId='" + SiteId + "';\r\n" +
                     "          document.getElementById(Id).style.display='none';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'false');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='block';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'true');\r\n" +
                     "      }\r\n" +
                     "  }\r\n" +
                     "</script>\r\n" +
                     "<h3><a href=\"javascript:CollapseExpandDocField('ulFilterDocField');\">Lĩnh vực</a></h3>\r\n" +
                     "<ul class=ulFilter id=ulFilterDocField style=\"display:" + DisplayDiv + "\">\r\n";
                for (int iIndex = 0; iIndex < DocFields.Length; iIndex++)
                {
                    if (DocField.Equals(DocFields[iIndex].DocFieldId))
                    {
                        Html += "   <li class=active><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&DocField=" + DocFields[iIndex].DocFieldId + "&gdf=" + gdf + "\">" + DocFields[iIndex].DocFieldName + "</a></li>\r\n";
                    }
                    else
                    {
                        Html += "   <li><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&DocField=" + DocFields[iIndex].DocFieldId + "&gdf=" + gdf + "\">" + DocFields[iIndex].DocFieldName + "</a></li>\r\n";
                    }
                }
                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }




        public static AjaxOut RenderDocYearRangeFilter(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);
                string YearRange = (string)WebEnvironments.Request(SiteId, "YearRange");
                if (YearRange == null) YearRange = "";


                string gdf = (string)WebEnvironments.Request(SiteId, "gdf");
                if (string.IsNullOrEmpty(gdf)) gdf = "";

                object DisplayDiv = WebSessionUtility.GetSession(SiteId, "ulFilterYearRange");
                if (DisplayDiv == null)
                {
                    DisplayDiv = "block";
                }
                else
                {
                    if ((bool)DisplayDiv)
                    {
                        DisplayDiv = "block";
                    }
                    else
                    {
                        DisplayDiv = "none";
                    }
                }

                DocYearRangeCls[] YearRanges = DocYearRangeProcessBll.ReadingDocYearRange(SiteId, OGroupData.GroupDataId);
                string Html =
                     "<script language=javascript>\r\n" +
                     "  function CollapseExpandYearRange(Id)\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      if(document.getElementById(Id).style.display=='block')\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='none';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'false');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          document.getElementById(Id).style.display='block';\r\n" +
                     "          Portal.Cms.Render.CmsDocumentDataRenderBll.SetCollapseExpand(SiteId,Id,'true');\r\n" +
                     "      }\r\n" +
                     "  }\r\n" +
                     "</script>\r\n" +
                     "<h3><a href=\"javascript:CollapseExpandYearRange('ulFilterYearRange');\">Năm ban hành</a></h3>\r\n" +
                     "<ul class=ulFilter id=ulFilterYearRange style=\"display:" + DisplayDiv + "\">\r\n";
                for (int iIndex = 0; iIndex < YearRanges.Length; iIndex++)
                {
                    if (YearRange.Equals(YearRanges[iIndex].DocYearRangeId))
                    {
                        Html += "   <li class=active><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&YearRange=" + YearRanges[iIndex].DocYearRangeId + "&gdf=" + gdf + "\">" + YearRanges[iIndex].DocYearRangeName + "</a></li>\r\n";
                    }
                    else
                    {
                        Html += "   <li><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&YearRange=" + YearRanges[iIndex].DocYearRangeId + "&gdf=" + gdf + "\">" + YearRanges[iIndex].DocYearRangeName + "</a></li>\r\n";
                    }
                }
                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        class SortFieldCls
        {
            public string Field;
            public string Name;

            public SortFieldCls(string _Field, string _Name)
            {
                Field = _Field;
                Name = _Name;
            }
        }

        class SortSizeCls
        {
            public string Field;
            public string Name;

            public SortSizeCls(string _Field, string _Name)
            {
                Field = _Field;
                Name = _Name;
            }
        }


        static string ProcessHighLight(string Subject, string Data)
        {
            if (string.IsNullOrEmpty(Subject))
            {
                return Data;
            }
            else
            {
                Data = Data.Replace(Subject, "<span style=\"background-color:yellow;color:maroon;padding:2px;\">" + Subject + "</span>");
                return Data;
            }
        }

        /// <summary>
        /// Ket qua tim kiem
        /// </summary>
        /// <param name="SiteId"></param>
        /// <param name="ORenderInfo"></param>
        /// <returns></returns>
        public static AjaxOut RenderSearchDocumentResult(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonUrl = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, gid, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                bool AllowManage = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcManage, gid, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                bool AllowPublish = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcPublish, gid, WebSessionUtility.GetCurrentLoginUser().UserId, false);

                SearchParamCls OSearchParam = new SearchParamCls();
                OSearchParam.DataRoot = WebConfig.DataPathRoot(OSiteParam);

                OSearchParam.ActiveGroupData = true;
                OSearchParam.GroupDataId = OGroupData.GroupDataId;
                OSearchParam.SearchDocumentNo = (string)WebEnvironments.Request(SiteId, "documentno");
                if (OSearchParam.SearchDocumentNo == null) OSearchParam.SearchDocumentNo = "";

                OSearchParam.SearchDocumentNo = OSearchParam.SearchDocumentNo.Trim();
                OSearchParam.ActiveSearchDocumentNo = !string.IsNullOrEmpty(OSearchParam.SearchDocumentNo);
                //OSearchParam.IndexesToSearch = @"E:\Projects\QNet.Portal\UniversityNet\UserDataStorage\DocumentArchives\pvfc\Xml\Index";
                //OSearchParam.IndexesToSearchAttachDoc = @"E:\Projects\QNet.Portal\UniversityNet\UserDataStorage\DocumentArchives\pvfc\ArchiveDoc\Index";

                string AssetCode = WebConfig.GetConfigFromXml(OSiteParam, "DefaultAssetCode");
                OSearchParam.IndexesToSearch = WebConfig.DataPathRoot(OSiteParam) + "\\DocumentArchives\\" + AssetCode + "\\Xml\\Index";
                OSearchParam.IndexesToSearchAttachDoc = WebConfig.DataPathRoot(OSiteParam) + "\\DocumentArchives\\" + AssetCode + "\\ArchiveDoc\\Index";


                try
                {
                    int iActiveFullTextSearch = int.Parse((string)WebEnvironments.Request(SiteId, "fulltextsearch"));
                    OSearchParam.ActiveFullTextSearch = iActiveFullTextSearch == 1;
                }
                catch { }

                OSearchParam.fullTextSearchKeyword = (string)WebEnvironments.Request(SiteId, "fullTextSearchKeyword");
                if (string.IsNullOrEmpty(OSearchParam.fullTextSearchKeyword))
                {
                    OSearchParam.ActiveFullTextSearch = false;
                }

                OSearchParam.SearchDocType = (string)WebEnvironments.Request(SiteId, "doctype");
                OSearchParam.ActiveSearchDocType = !string.IsNullOrEmpty(OSearchParam.SearchDocType);

                string ip = (string)WebEnvironments.Request(SiteId, "ip");
                if (string.IsNullOrEmpty(ip)) ip = "2";
                OSearchParam.PublishingStatus = int.Parse(ip);

                string sab = (string)WebEnvironments.Request(SiteId, "sab");
                if (string.IsNullOrEmpty(sab)) sab = "2";
                OSearchParam.Searchable = int.Parse(sab);

                OSearchParam.SearchDocField = (string)WebEnvironments.Request(SiteId, "docfield");
                OSearchParam.ActiveSearchDocField = !string.IsNullOrEmpty(OSearchParam.SearchDocField);

                OSearchParam.SearchDocReleaseFrom = (string)WebEnvironments.Request(SiteId, "releasefrom");
                OSearchParam.ActiveSearchDocReleaseFrom = !string.IsNullOrEmpty(OSearchParam.SearchDocReleaseFrom);

                OSearchParam.SearchDocSigner = (string)WebEnvironments.Request(SiteId, "signer");
                OSearchParam.ActiveSearchDocSigner = !string.IsNullOrEmpty(OSearchParam.SearchDocSigner);

                OSearchParam.GroupDataFolderId = (string)WebEnvironments.Request(SiteId, "gdf");
                if (string.IsNullOrEmpty(OSearchParam.GroupDataFolderId)) OSearchParam.GroupDataFolderId = "";
                if (OSearchParam.GroupDataFolderId.Equals("root")) OSearchParam.GroupDataFolderId = "";
                OSearchParam.ActiveGroupDataFolderId = !string.IsNullOrEmpty(OSearchParam.GroupDataFolderId);

                string SearchYearRange = (string)WebEnvironments.Request(SiteId, "YearRange");
                if (!string.IsNullOrEmpty(SearchYearRange))
                {
                    DocYearRangeCls ODocYearRange = DocYearRangeProcessBll.CreateDocYearRangeModel(SiteId, SearchYearRange);
                    OSearchParam.ActiveSearchYearRange = true;
                    OSearchParam.SearchYearRangeFrom = ODocYearRange.FromYear.ToString();
                    OSearchParam.SearchYearRangeTo = ODocYearRange.ToYear.ToString();
                }

                OSearchParam.SearchSubject = (string)WebEnvironments.Request(SiteId, "subject");
                if (OSearchParam.SearchSubject == null) OSearchParam.SearchSubject = "";
                OSearchParam.SearchSubject = OSearchParam.SearchSubject.Trim();
                OSearchParam.ActiveSearchSubject = !string.IsNullOrEmpty(OSearchParam.SearchSubject);

                OSearchParam.SortField = (string)WebEnvironments.Request(SiteId, "SortField");
                if (string.IsNullOrEmpty(OSearchParam.SortField)) OSearchParam.SortField = "ReleaseDate";
                OSearchParam.SortType = (string)WebEnvironments.Request(SiteId, "SortType");
                if (string.IsNullOrEmpty(OSearchParam.SortType)) OSearchParam.SortType = "DESC";

                //try
                //{
                //    //111
                //    OSearchParam.VnFromSignDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "sfd")).ToString("yyyy/MM/dd");
                //    OSearchParam.VnToSignDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "std")).ToString("yyyy/MM/dd");
                //    OSearchParam.SearchSignDate = !string.IsNullOrEmpty(OSearchParam.VnFromSignDate) && !string.IsNullOrEmpty(OSearchParam.VnToSignDate);
                //}
                //catch { }


                try
                {
                    OSearchParam.VnFromReleaseDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "rfd")).ToString("yyyy/MM/dd");
                    OSearchParam.VnToReleaseDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "rtd")).ToString("yyyy/MM/dd");
                    OSearchParam.SearchReleaseDate = !string.IsNullOrEmpty(OSearchParam.VnFromReleaseDate) && !string.IsNullOrEmpty(OSearchParam.VnToReleaseDate);
                }
                catch { }


                try
                {
                    OSearchParam.VnFromEffectedDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "efd")).ToString("yyyy/MM/dd");
                    OSearchParam.VnToEffectedDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "etd")).ToString("yyyy/MM/dd");
                    OSearchParam.SearchEffectedDate = !string.IsNullOrEmpty(OSearchParam.VnFromEffectedDate) && !string.IsNullOrEmpty(OSearchParam.VnToEffectedDate);
                }
                catch { }



                //try
                //{
                //    OSearchParam.VnFromEffectedDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "efd")).ToString("yyyy/MM/dd");
                //    OSearchParam.VnToEffectedDate = FunctionUtilities.VNDateToDate((string)WebEnvironments.Request(SiteId, "etd")).ToString("yyyy/MM/dd");
                //    OSearchParam.SearchEffectedDate = !string.IsNullOrEmpty(OSearchParam.VnFromEffectedDate) && !string.IsNullOrEmpty(OSearchParam.VnToEffectedDate);
                //}
                //catch { }


                string sortValue = string.Empty;

                try
                {
                    sortValue = WebEnvironments.Request(SiteId, "SortField");
                    sortValue += " " + WebEnvironments.Request(SiteId, "SortType");
                }
                catch { sortValue = string.Empty; }

                OSearchParam.PageSize = 10;
                try
                {
                    OSearchParam.PageSize = int.Parse(WebEnvironments.Request(SiteId, "PageSize"));
                }
                catch { }
                int PageIndex = 0;
                try
                {
                    PageIndex = int.Parse(WebEnvironments.Request(SiteId, "PageIndex"));
                }
                catch { }
                OSearchParam.PageIndex = PageIndex;
                string isPublic = "2";
                try
                {
                    isPublic = WebEnvironments.Request(SiteId, "ps");
                }
                catch
                {
                    OSearchParam.PublishingStatus = 2;
                }
                //TODO:  search form index file
                //SearchResultCls OSearchResult = SearchBll.Execute(SiteId, OSearchParam);
                UserCls _UserCls = WebSessionUtility.GetCurrentLoginUser();
                //TODO: get permission by workflowdefid and currentUser
                //fix group
                string loginname = _UserCls.LoginName;
                if (!_UserCls.RoleId.Equals("input"))
                    loginname = string.Empty;

                CmsDataCls[] OSearchResult = new CmsProcessBll().SearchAdvancePublicCmsData(
                    SiteId,
                    OSearchParam.GroupDataId,
                    string.Empty,
                    loginname,
                    isPublic,
                    sortValue,
                    OSearchParam.SearchDocumentNo,
                    OSearchParam.SearchSubject,
                    OSearchParam.VnFromSignDate,
                    OSearchParam.VnToSignDate,
                    OSearchParam.VnFromReleaseDate,
                    OSearchParam.VnToReleaseDate,
                    OSearchParam.VnFromEffectedDate,
                    OSearchParam.VnToEffectedDate,                    
                    OSearchParam.SearchDocReleaseFrom,
                    OSearchParam.SearchDocField,
                    OSearchParam.SearchDocType,
                    OSearchParam.SearchDocSigner
                    );


                //CmsDataCls[] OSearchResult = new CmsProcessBll().ReadingCmsDataIsPublicByUser(
                //    SiteId,
                //    OSearchParam.GroupDataId,
                //    string.Empty,
                //    loginname,
                //    isPublic,
                //    sortValue,
                //    OSearchParam.SearchDocReleaseFrom,
                //    OSearchParam.SearchDocField,
                //    OSearchParam.SearchDocType,
                //    OSearchParam.SearchDocSigner,
                //    OSearchParam.SearchDocumentNo);      

                //ReturnPaging OReturnPaging = WebPaging.GetPaging(OSearchResult.SearchResultItems.Count, PageIndex, OSearchParam.PageSize, 10, "NextPage", 0);
                ReturnPaging OReturnPaging = WebPaging.GetPaging(OSearchResult.Length, PageIndex, OSearchParam.PageSize, 10, "NextPage", 0);
                SortFieldCls[] SortFields = new SortFieldCls[]{
                    new SortFieldCls("Subject","Số văn bản"),
                    //new SortFieldCls("Subject","Tiêu đề"),
                    new SortFieldCls("frkDocTypeId","Loại văn bản"),
                    new SortFieldCls("frkDocFieldId","Lĩnh vực"),
                    new SortFieldCls("frkDocReleaseFromId","Cơ quan ban hành"),
                    new SortFieldCls("frkDocSignerId","Người ký"),
                    //new SortFieldCls("SignDate","Ngày ký"),
                    new SortFieldCls("ReleaseDate","Ngày ban hành"),
                    new SortFieldCls("EffectedDate","Ngày hiệu lực"),
                };
                string SelectSortFieldText =
                    "<select id=drpSelectSortField class=drpselect>\r\n";
                for (int iIndex = 0; iIndex < SortFields.Length; iIndex++)
                {
                    if (OSearchParam.SortField.Equals(SortFields[iIndex].Field))
                    {
                        SelectSortFieldText += "  <option selected value=\"" + SortFields[iIndex].Field + "\">" + SortFields[iIndex].Name + "</option>\r\n";
                    }
                    else
                    {
                        SelectSortFieldText += "  <option value=\"" + SortFields[iIndex].Field + "\">" + SortFields[iIndex].Name + "</option>\r\n";
                    }
                }
                SelectSortFieldText += "</select>\r\n";

                SortSizeCls[] SortSize = new SortSizeCls[]{
                    new SortSizeCls("5","05"),
                    new SortSizeCls("10","10"),
                    new SortSizeCls("15","15"),
                    new SortSizeCls("20","20"),
                    new SortSizeCls("25","25"),
                };


                string SelectSortSizeText =
                  "<select id=drpSelectPageSize class=drpselect>\r\n";
                for (int iIndex = 0; iIndex < SortSize.Length; iIndex++)
                {
                    if (OSearchParam.PageSize == int.Parse(SortSize[iIndex].Field))
                    {
                        SelectSortSizeText += "  <option selected value=\"" + SortSize[iIndex].Field + "\">" + SortSize[iIndex].Name + "</option>\r\n";
                    }
                    else
                    {
                        SelectSortSizeText += "  <option value=\"" + SortSize[iIndex].Field + "\">" + SortSize[iIndex].Name + "</option>\r\n";
                    }
                }
                SelectSortSizeText += "</select>\r\n";

                string SelectSortType = " <select id=drpSortType class=drpselect>\r\n" +
                                        "   <option value=\"ASC\">Sắp tăng dần</option>\r\n" +
                                        "   <option value=\"DESC\">Sắp giảm dần</option>\r\n" +
                                        " </select>\r\n";
                if (OSearchParam.SortType.Equals("DESC"))
                {
                    SelectSortType = " <select id=drpSortType class=drpselect>\r\n" +
                                        "   <option value=\"ASC\">Sắp tăng dần</option>\r\n" +
                                        "   <option selected value=\"DESC\">Sắp giảm dần</option>\r\n" +
                                        " </select>\r\n";
                }

                string SelectStatus =
                    " <select id=drpSelectStatus class=drpselect>\r\n" +
                    "   <option value=\"2\">Toàn bộ</option>\r\n" +
                    "   <option value=\"0\">Chờ duyệt</option>\r\n" +
                    "   <option value=\"1\">Đã duyệt</option>\r\n" +
                    "   <option value=\"1\">Hủy đăng</option>\r\n" +
                    " </select>\r\n";



                if (OSearchParam.PublishingStatus == 0)
                {
                    SelectStatus =
                    " <select id=drpSelectStatus class=drpselect>\r\n" +
                    "   <option value=\"2\">Toàn bộ</option>\r\n" +
                    "   <option value=\"0\" selected>Chờ duyệt</option>\r\n" +
                    "   <option value=\"1\">Đã duyệt</option>\r\n" +
                    " </select>\r\n";
                }

                if (OSearchParam.PublishingStatus == 1)
                {
                    SelectStatus =
                    " <select id=drpSelectStatus class=drpselect>\r\n" +
                    "   <option value=\"2\">Toàn bộ</option>\r\n" +
                    "   <option value=\"0\">Chờ duyệt</option>\r\n" +
                    "   <option value=\"1\" selected>Đã duyệt</option>\r\n" +
                    " </select>\r\n";
                }

                string Html =
                    " <script language=javascript>\r\n" +
                    "   function ProcessDownload(DocumentId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       OAjaxOut=Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideDownloadDocument(SiteId,DocumentId).value;\r\n" +
                    "       if(OAjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(OAjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(OAjaxOut.RetUrl,'_blank');\r\n" +
                    "   }\r\n" +

                    "   function NextPage(CurrentPageIndex)\r\n" +
                    "   {\r\n" +
                    "       ClientDoSearchDocument(CurrentPageIndex);\r\n" +
                    "   }\r\n" +


                    "   function ClientRequestPublish(DocumentId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       if(confirm('Bạn đã chắc chắn gửi duyệt phát hành chưa?')==false)return;\r\n" +
                    "       document.getElementById('divSendApproved'+DocumentId).innerHTML='Đang xử lý...';\r\n" +
                    "       setTimeout('RealClientRequestPublish(\"'+DocumentId+'\")',10);\r\n" +
                    "   }\r\n" +

                    "   function RealClientRequestPublish(DocumentId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       OAjaxOut=Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideSendPublish(SiteId,DocumentId).value;\r\n" +
                    "       document.getElementById('divSendApproved'+DocumentId).innerHTML='';\r\n" +
                    "       if(OAjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(OAjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divSendPublish'+DocumentId).innerHTML='';\r\n" +
                    "       alert('Đã gửi vào danh sách chờ duyệt và phát hành!');\r\n" +
                    "   }\r\n" +

                    "   function ClientAddShortCut(GroupDataId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       OAjaxOut=Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideAddShortCut(SiteId,'document',GroupDataId).value;\r\n" +
                    "       if(OAjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(OAjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       alert(OAjaxOut.InfoMessage);\r\n" +
                    "   }\r\n" +

                    " </script>\r\n";

                string PagingText = OReturnPaging.PagingText;// OSearchResult.PaggingText;
                string ParentDataId = (string)WebEnvironments.Request(SiteId, "parent");

                Html +=
                    GetPlugInJavascript(SiteId, ParentDataId) +
                    //" <div id=divActionUpdateForm style=\"display:none;\">" + DrawUpdateForm(SiteId, gid).HtmlContent + "</div>\r\n" +
                    //" <div id=divActionAddForm style=\"display:none;\">" + DrawAddForm(SiteId, gid).HtmlContent + "</div>\r\n" +
                    " <div id=divActionChangeAvatarForm  style=\"display:none;\">" + DrawChangeAvatarForm(SiteId, gid).HtmlContent + "</div>\r\n" +
                    " <div id=divActionAttachDocForm  style=\"display:none;\">" + DrawChangeAttachDocForm(SiteId, gid).HtmlContent + "</div>\r\n" +

                 " <div class=CssListCategory id=divListForm>\r\n";
                if (AllowManage)
                {
                    Html +=
                        " <div class=RelationFunction>\r\n" +
                        "    <ul>\r\n" +
                        //"        <li class=CssliFolder><img class=imagebutton src=\"" + ButtonUrl + "/cms/folder.png\"><a href=\"GroupDataFolder.aspx?gid=" + OGroupData.GroupDataId + "\"><div>Thư mục</div></a></li>\r\n" +
                        "        <li class=CssliDocumentType><img class=imagebutton src=\"" + ButtonUrl + "/cms/menu.png\"><a href=\"DocumentType.aspx?gid=" + OGroupData.GroupDataId + "\"><div>Loại văn bản</div></a></li>\r\n" +
                        "        <li class=CssliDocumentSigner><img class=imagebutton src=\"" + ButtonUrl + "/cms/signer.png\"><a href=\"DocumentSigner.aspx?gid=" + OGroupData.GroupDataId + "\"><div>Người ký văn bản</div></a></li>\r\n" +
                        "        <li class=CssliDocumentReleaseFrom><img class=imagebutton src=\"" + ButtonUrl + "/cms/Office.png\"><div><a href=\"DocumentReleaseFrom.aspx?gid=" + OGroupData.GroupDataId + "\">Cơ quan ban hành</div></a></li>\r\n" +
                        "        <li class=CssliDocumentField><img class=imagebutton src=\"" + ButtonUrl + "/cms/Field.png\"><a href=\"DocumentField.aspx?gid=" + OGroupData.GroupDataId + "\"><div>Lĩnh vực văn bản</div></a></li>\r\n" +
                        "        <li class=CssliDocumentRange><img class=imagebutton src=\"" + ButtonUrl + "/cms/Year.png\"><a href=\"DocumentYearRange.aspx?gid=" + OGroupData.GroupDataId + "\"><div>Khoảng thời gian</div></a></li>\r\n" +
                        "        <li class=CssliDocumentRelation><img class=imagebutton src=\"" + ButtonUrl + "/cms/DocRelationType.png\"><a href=\"DocumentRelationType.aspx?gid=" + OGroupData.GroupDataId + "\"><div>Kiểu quan hệ</div></a></li>\r\n" +
                        "        <li class=CssliDocumentIndex><img class=imagebutton src=\"" + ButtonUrl + "/cms/Shortcut.png\"><a href=\"javascript:ClientUpdateIndex('" + OGroupData.GroupDataId + "');\"><div>Index tìm kiếm</div></a></li>\r\n" +
                        "        <li class=CssliDocumentAdd><img class=imagebutton src=\"" + ButtonUrl + "/cms/Add.png\"><a href=\"javascript:ClientAdd('" + OGroupData.GroupDataId + "');\"><div>Tạo mới văn bản</div></a></li>\r\n" +
                        "        <li class=CssliDocumentBack><img class=imagebutton src=\"" + ButtonUrl + "/cms/Back.png\"><a href=\"javascript:window.open('../../Folder.aspx','_self');\"><div>Về trang chủ</div></a></li>\r\n" +
                        "    </ul>\r\n" +
                        "</div>\r\n";
                }

                string AppendTitle = "";
                if (!string.IsNullOrEmpty(OSearchParam.GroupDataFolderId))
                {
                    GroupDataFolderCls OGroupDataFolder = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataFolderModel(null, SiteId, OSearchParam.GroupDataFolderId);
                    AppendTitle = " - " + OGroupDataFolder.FolderName;
                }
                Html +=
                    " <h3>" + OGroupData.GroupDataName + AppendTitle + "</h3>\r\n" +
                    " <div class=CssNewsHeading>Tổng số có " + OSearchResult.Length + " văn bản</div>\r\n" +
                    " <div class=CssSort>Trạng thái: " + SelectStatus + " - Sắp xếp: " + SelectSortFieldText + " - Kiểu: " + SelectSortType + " - văn bản " + SelectSortSizeText + " / trang <input type=button class=button value=\"Lọc\" onclick=\"javascript:NextPage(0);\"></div>\r\n" +
                     PagingText +
                    " <div class=CssResultItems>\r\n" +
                    " <table> \r\n";

                string WebPartLayoutButtonUrl = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);
                int CountIndex = OReturnPaging.StartIndex;// OSearchResult.StartIndex;
                string Url = "";
                for (int iIndex = OReturnPaging.StartIndex; iIndex < OReturnPaging.EndIndex; iIndex++)
                {

                    ///Add new to check per
                    bool hasPer = true;
                    CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, OSearchResult[iIndex].DataId);
                    if (OCmsData == null) throw new Exception("Tham số truy cập không hợp lệ. Bài viết không tìm thấy hoặc đã được xóa!");
                    string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");

                    bool HasStarted = WorkflowUtilities.CheckHasStartWorkflow(SiteId, OSearchResult[iIndex].DataId);
                    if (HasStarted == false)
                    {
                        //neu chua nam trong quy trinh thi kiem tra binh thuong
                        SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                    }
                    else
                    {
                        //net nam trong quy trinh
                        string WorkflowDefId = OCmsData.WorkflowDefId;
                        if (!string.IsNullOrEmpty(WorkflowDefId))//co quy trinh
                        {
                            Portal.Workflow.Bussiness.WorkflowBll.GetWorkflowCls
                                  OWorkFlowInfo = WorkflowBll.GetWorkflowActionInfo(OSiteParam.ConnectionString, WorkflowDefId, OSearchResult[iIndex].DataId);
                            //lay thong tin
                            if (OWorkFlowInfo == null)
                            {
                                //neu khong duoc chuyen thi la chua den luot minh
                                //throw new Exception("Bài viết đang nằm trong quy trình và bạn không có quyền sửa");
                                hasPer = false;
                            }
                            else
                            {
                                if (OWorkFlowInfo.IsViewer)
                                {
                                    //da duoc xu ly
                                    throw new Exception("Bài viết  trong quy trình và đã được người khác lấy về xử lý bạn không có quyền sửa");
                                }
                            }
                        }
                        else
                        {
                            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                        }
                    }
                    ///end check

                    string NewImageText = "";
                    if (OSearchResult[iIndex].IsNew == 1)
                    {
                        //NewImageText = "<img class=imgthumb src=\"Resources/Images/Newred.png\"></img>";
                    }
                    string ReleaseButton = "";                  

                    /// Bo chuc nang duyet o day
                    if (AllowPublish)
                    {
                        //ReleaseButton =
                        //                          "<input type=button class=button value=\"Phát hành\" onclick=\"javascript:ClientPublic('" + OSearchResult.SearchResultItems[iIndex].DocumentId + "');\">";
                        if (OSearchResult[iIndex].IsPublic == 1)
                        {
                            ReleaseButton =
                                "<input type=button class=button value=\"Hủy đăng\" onclick=\"javascript:ClientUnPublic('" + OSearchResult[iIndex].DataId + "');\">";
                        }
                        else
                        {
                            if (hasPer)
                            {
                                ReleaseButton =
                                    "<input type=button class=button value=\"Duyệt\" onclick=\"javascript:ClientPublic('" + OSearchResult[iIndex].DataId + "');\">";
                            }
                        }
                    }
                    string SubMenuButton = "";
                    if (OGroupData.AllowSub == 1)
                    {
                        SubMenuButton =
                            "<input style=\"width:80px;font-size:12px;\" type=button class=button value=\"Dữ liệu con\" onclick=\"javascript:window.open('InputData.aspx?gid=" + OGroupData.GroupDataId + "&parent=" + OSearchResult[iIndex].DataId + "','_self');\">";
                    }

                    Url = Utility.GetUrlDetails(OSearchResult[iIndex].Subject, OSearchResult[iIndex].DataId, SiteId, string.Empty);
                    Html +=
                        " <tr> \r\n" +
                        "    <td style=\"width:40px;vertical-align:top\">" + NewImageText + "</td> \r\n" +
                        "    <td> \r\n" +
                        "        <div class=NewsTitle><a href=\"" + Url + "\">" + (CountIndex + 1) + ". " + ProcessHighLight(OSearchParam.SearchSubject, OSearchResult[iIndex].Subject) + "</a></div> \r\n" +
                        "        <div>Số: <span class=DocNo>" + ProcessHighLight(OSearchParam.SearchDocumentNo, OSearchResult[iIndex].Subject) + "</span></div> \r\n" +
                        "        <div>Loại văn bản: <span class=ReleaseFrom>" + OSearchResult[iIndex].DocTypeName + "</span></div> \r\n" +
                        "        <div>Đơn vị ban hành: <span class=ReleaseFrom>" + OSearchResult[iIndex].DocReleaseFromName + "</span></div> \r\n" +
                        "        <div>Người ký: <span class=ReleaseFrom>" + OSearchResult[iIndex].DocSignerName + "</span></div> \r\n" +
                        "        <div>Chức vụ: <span class=ReleaseFrom>" + OSearchResult[iIndex].JobTitle + "</span></div> \r\n" +                       
                        "        <div>Ban hành ngày <span class=CssDate>" + OSearchResult[iIndex].ReleaseDate + "</span> | Hiệu lực <span class=CssDate>" + OSearchResult[iIndex].EffectedDate + "</span></div> \r\n" +
                        "        <div><span class=CssTrichYeu>Trích yếu: </span><span class=CssTrichYeuDetail>" + ProcessHighLight(OSearchParam.SearchSubject, OSearchResult[iIndex].Description) + "</span></div> \r\n" +

                        //  "        <div>Loại văn bản: <span class=ReleaseFrom><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&doctype=" + OSearchResult[iIndex].frkDocTypeId + "\">" + OSearchResult[iIndex].DocTypeName + "</a></span></div> \r\n" +
                        //"        <div>Đơn vị ban hành: <span class=ReleaseFrom><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&ReleaseFrom=" + OSearchResult[iIndex].frkDocReleaseFromId + "\">" + OSearchResult[iIndex].DocReleaseFromName + "</a></span></div> \r\n" +
                        //"        <div>Người ký: <span class=ReleaseFrom><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&singer=" + OSearchResult[iIndex].frkDocSignerId + "\">" + OSearchResult[iIndex].DocSignerName + "</a></span></div> \r\n" +
                        //"        <div>Chức vụ: <span class=ReleaseFrom><a href=\"" + ORenderInfo.JumpPageAspx + "?gid=" + gid + "&singer=" + OSearchResult[iIndex].frkDocSignerId + "\">" + OSearchResult[iIndex].JobTitle + "</a></span></div> \r\n" +
                        //"        <div>Ban hành ngày <span class=CssDate>" + OSearchResult[iIndex].ReleaseDate + "</span> | Hiệu lực <span class=CssDate>" + OSearchResult[iIndex].EffectedDate + "</span></div> \r\n" +
                        //"        <div><span class=CssTrichYeu>Trích yếu: </span><span class=CssTrichYeuDetail>" + ProcessHighLight(OSearchParam.SearchSubject, OSearchResult[iIndex].Description) + "</span></div> \r\n" +

                        "        <div class=CssStatusText id=\"divStatusText" + OSearchResult[iIndex].DataId + "\">" + (OSearchResult[iIndex].IsPublic == 1 ? "Đã duyệt" : "Chờ duyệt") + "</div>\r\n" +
                        "        <div id=\"divSendApproved" + OSearchResult[iIndex].DataId + "\" class=\"CssProcessing\"></div>\r\n" +
                        "        <div class=download>\r\n" +
                        "           <table>\r\n" +
                        "               <tr>\r\n" +
                        "                   <td style=\"vertical-align:top;padding-top:5px;margin-left:10px;\"><input onclick=\"javascript:ProcessDownload('" + OSearchResult[iIndex].DataId + "');\" value=\"Download tài liệu\" type=button class=button></td>\r\n" +
                        "                   <td style=\"vertical-align:top;padding-top:5px;margin-left:10px;\"><div id=divStatusText" + OSearchResult[iIndex].DataId + "><div id=\"divButton" + OSearchResult[iIndex].DataId + "\">" + ReleaseButton + "</div></div></td>\r\n";

                    bool IsPublic = false;
                    if (OSearchResult[iIndex].IsPublic == 1)
                        IsPublic = true;

                    if (AllowManage)
                    {
                        if (hasPer)
                        {
                            if (!IsPublic)
                            {
                                string UrlUpdate = Utility.GetUrlUpdate("update", OSearchResult[iIndex].DataId, OSiteParam);
                                Html +=
                                        "<td style=\"vertical-align:top;padding-top:5px;margin-left:10px;\"><input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + gid + "','" + OSearchResult[iIndex].DataId + "','" + UrlUpdate + "');\"></td>\r\n";
                            }
                        }
                        if ((AllowPublish == true && OSearchResult[iIndex].IsPublic == 1) || (AllowPublish == false && OSearchResult[iIndex].IsPublic != 1 && OSearchResult[iIndex].EntryBy.Equals(loginname)))
                        {
                            Html +=
                                    "<td style=\"vertical-align:top;padding-top:5px;margin-left:10px;\"><input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + gid + "','" + OSearchResult[iIndex].DataId + "');\"></td>\r\n" +
                                    "<td style=\"vertical-align:top;padding-top:5px;margin-left:10px;\">" + SubMenuButton + "</td>\r\n";
                        }
                    }
                    Html +=
                        "               </tr>\r\n" +
                        "           </table>\r\n" +
                        "       </div> \r\n" +
                        "   </td> \r\n" +
                        "</tr> \r\n";
                    CountIndex++;
                }
                Html +=
                    "</table>\r\n" +
                    "</div>\r\n" +
                    PagingText +

                    " <div class=\"clr\"></div>\r\n" +
                    " </div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static void SetCollapseExpand(string SiteId, string DivId, string Expand)
        {
            WebSessionUtility.SetSession(SiteId, DivId, bool.Parse(Expand));
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDownloadAttachFile(string SiteId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DataId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                OAjaxOut.RetUrl = OCmsData.GetAttachDocUrl(SiteId, WebConfig.HttpDataRoot(OSiteParam));
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        public static AjaxOut ViewDocumentDetail(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Id = (string)WebEnvironments.Request(SiteId, "Id");
                string Back = (string)WebEnvironments.Request(SiteId, "Back");
                if (string.IsNullOrEmpty(Id)) throw new Exception("Tài liệu không tìm thấy! Truy vấn không hợp lệ");
                if (string.IsNullOrEmpty(Back)) throw new Exception("Tham số truy vấn không hợp lệ");
                string gid = (string)WebEnvironments.Request(SiteId, "gid");

                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, Id, true, false);
                if (OCmsData == null) throw new Exception("Tài liệu không tìm thấy! Truy vấn không hợp lệ");
                string BackUrl = Back + "?gid=" + gid;
                string BackAspx = ORenderInfo.JumpPageAspx;
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string WebPartLayoutButtonUrl = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);

                string NewImageText = "";
                if (OCmsData.IsNew == 1)
                {
                    //NewImageText = "<img class=imgthumb src=\"Resources/Images/Newred.png\"></img>";
                }

                string ButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                string LiProcessWorkflow = "<li><img class=imagebutton src=\"" + ButtonUrl + "/Workflow.png\"><a href=\"javascript:ClientStartWorkflow('" + OCmsData.DataId + "');\"><div>Khởi tạo quy trình</div></a></li>\r\n";
                bool HasStarted = WorkflowUtilities.CheckHasStartWorkflow(SiteId, OCmsData.DataId);
                if (HasStarted)
                {
                    Portal.Workflow.Bussiness.WorkflowBll.GetWorkflowCls
                        OWorkflow = WorkflowUtilities.GetWorkflowActionInfo(SiteId, OCmsData.DataId);
                    if (OWorkflow != null)
                    {
                        LiProcessWorkflow = "<li><img class=imagebutton src=\"" + ButtonUrl + "/Workflow.png\"><a href=\"javascript:ProcessWorkflow('" + OWorkflow.WorkflowDefId + "','" + OWorkflow.WorkflowActionId + "','" + OCmsData.DataId + "');\"><div>Xử lý quy trình</div></a></li>";
                    }
                    else
                    {
                        LiProcessWorkflow = "";
                    }
                }

                string Html =
                    " <script language=javascript>\r\n" +
                    "   function ClientAddShortCut(DataId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       OAjaxOut=Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideAddShortCutView(SiteId,'ViewDocument',DataId).value;\r\n" +
                    "       if(OAjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(OAjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       alert(OAjaxOut.InfoMessage);\r\n" +
                    "   }\r\n" +

                    "   function ClientDownloadAttachFile(DataId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideDownloadAttachFile(SiteId,DataId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_blank');\r\n" +
                    "   }\r\n" +

                    "   function ClientAddRelationDocument(GroupDataId,DataId,RelationTypeId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       documentNo=document.getElementById('txtRelationTypeText'+RelationTypeId).value;\r\n" +
                    "       documentNo=documentNo.trim();\r\n" +
                    "       if(documentNo=='')\r\n" +
                    "       {\r\n" +
                    "           alert('Chưa nhập số văn bản liên quan!');\r\n" +
                    "           document.getElementById('txtRelationTypeText'+RelationTypeId).focus();\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideAddRelationDocument(SiteId,GroupDataId, DataId, RelationTypeId, documentNo, '" + BackAspx + "').value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientDeleteRelation(RelationDataId,GroupDataId,DataId,RelationTypeId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                    "       AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideDeleteRelationDocument(SiteId,RelationDataId, GroupDataId, DataId, RelationTypeId, '" + BackAspx + "').value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientStartWorkflow(DataId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       if(confirm('Bạn đã chắc chắn khởi tạo quy trình chưa?')==false)return;\r\n" +
                    "       AjaxOut = Portal.Cms.Render.CmsDocumentDataRenderBll.`(SiteId, DataId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "   }\r\n" +

                    "    function ProcessWorkflow(WorkflowDefId,WorkflowActionId,RecordId)\r\n" +
                    "    {\r\n" +
                    "        SiteId='" + SiteId + "';\r\n" +
                    "        AjaxOut=Portal.Cms.Render.CmsDocumentDataRenderBll.ServerSideProcessAjaxNextLink(SiteId,WorkflowDefId,WorkflowActionId,RecordId).value;\r\n" +
                    "        document.getElementById('content_workflow').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "        document.getElementById('content_workflow').style.display='block';\r\n" +
                    //"        document.getElementById('divMainForm').style.display='none';\r\n" +
                    "    }\r\n" +
                    " </script>\r\n" +

                     WorkflowBll.ServerSideGetPlugInJavascriptDoNextLink(SiteId, "content_workflow", "divMainForm") +
                    " <div id=divMainForm>\r\n" +
                    " <div class=RelationFunction>\r\n" +
                    "    <ul>\r\n" +
                    "        <li><img class=imagebutton src=\"" + ButtonUrl + "/Back.png\"><a href=\"javascript:window.open('" + BackUrl + "','_self');\"><div>Quay lại</div></a></li>\r\n" +
                    "        <li><img class=imagebutton src=\"" + ButtonUrl + "/Shortcut.png\"><a href=\"javascript:ClientAddShortCut('" + OCmsData.DataId + "');\"><div>Tạo shortcut</div></a></li>\r\n" +
                    LiProcessWorkflow +
                    "    </ul>\r\n" +
                    " </div>\r\n" +

                    " <div class=CssListCategory> \r\n" +
                    " <h3>" + OCmsData.Subject + "</h3>\r\n" +
                    //" <div class=function style=\"text-align:right;margin-right:10px;\"><input type=button class=button onclick=\"javascript:window.open('" + BackUrl + "','_self');\" value=\"Quay lại\"></div> \r\n" +
                    " <table> \r\n" +
                    "     <tr> \r\n" +
                    "         <td style=\"width:40px;vertical-align:top\">" + NewImageText + "</td> \r\n" +
                    "         <td> \r\n" +
                    "             <div class=viewdetail> \r\n" +
                    "             <table> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Số văn bản:</td> \r\n" +
                    "                     <td class=col2><span class=DocNo>" + OCmsData.DataAlias + "</span></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Tiêu đề văn bản:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.Subject + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Lĩnh vực:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.DocFieldName + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Loại văn bản:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.DocTypeName + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Đơn vị ban hành:</td> \r\n" +
                    "                     <td class=col2>" + OCmsData.DocReleaseFromName + "</td> \r\n" +
                    "                 </tr> \r\n" +
                    //"                 <tr> \r\n" +
                    //"                     <td class=col1>Ngày ký:</td> \r\n" +
                    //"                     <td class=col2><span class=CssDate>" + OCmsData.SignDate + "</span</td> \r\n" +
                    //"                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Ngày ban hành:</td> \r\n" +
                    "                     <td class=col2><span class=CssDate>" + OCmsData.ReleaseDate + "</span</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Ngày hiệu lực:</td> \r\n" +
                    "                     <td class=col2><span class=CssDate>" + OCmsData.EffectedDate + "</span</td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Thay thế văn bản:</td> \r\n" +
                    "                     <td class=col2></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Sửa chữa văn bản:</td> \r\n" +
                    "                     <td class=col2></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Trích yếu:</td> \r\n" +
                    "                     <td class=col2><span class=CssTrichYeuDetail>" + OCmsData.Description + "</span></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1></td> \r\n" +
                    "                     <td class=col2>Số lần xem: <span class=CssTotalView>" + OCmsData.TotalPreview.ToString("#,##0") + "</span> | Số lần tải: <span class=CssTotalDownload>" + OCmsData.TotalDownload.ToString("#,##0") + "</span></td> \r\n" +
                    "                 </tr> \r\n" +
                    "                 <tr> \r\n" +
                    "                     <td class=col1>Tải về máy:</td> \r\n" +
                    "                     <td class=col2><img onclick=\"javascript:ClientDownloadAttachFile('" + OCmsData.DataId + "');\" style=\"border:none;\" alt=\"Download tài liệu gắn kèm\" title=\"Download tài liệu gắn kèm\" class=image src=\"" + WebPartLayoutButtonUrl + "/cms/download.png\"></div></td> \r\n" +
                    "                 </tr> \r\n" +
                    "             </table> \r\n" +
                    "             </div> \r\n" +
                    "         </td> \r\n" +
                    "     </tr> \r\n" +
                    "</table> \r\n";
                bool AllowManage = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcManage, gid, WebSessionUtility.GetCurrentLoginUser().UserId, false);
                if (AllowManage)
                {
                    RelationTypeCls[] RelationTypes = RelationTypeProcessBll.ReadingRelationType(SiteId, OCmsData.frkGroupDataId);
                    for (int iIndex = 0; iIndex < RelationTypes.Length; iIndex++)
                    {
                        CmsDataRelationCls[] CmsDataRelations = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDocumentRelation(SiteId, OCmsData.DataId, RelationTypes[iIndex].RelationTypeId);
                        Html +=
                        " <h3>" + RelationTypes[iIndex].RelationTypeName + "</h3>\r\n" +
                        " <div class=CssRelationDivFunc>Số văn bản: <input type=textbox id=\"txtRelationTypeText" + RelationTypes[iIndex].RelationTypeId + "\" class=textbox><input type=button class=button value=\"Đưa vào\" onclick=\"javascript:ClientAddRelationDocument('" + OCmsData.frkGroupDataId + "','" + OCmsData.DataId + "','" + RelationTypes[iIndex].RelationTypeId + "');\"></div>\r\n" +
                        " <div class=relationdoc>\r\n" +
                        " <ul>\r\n";
                        for (int zIndex = 0; zIndex < CmsDataRelations.Length; zIndex++)
                        {
                            Html +=
                                "<li>\r\n" +
                                "   <img class=\"imgdelete\" onclick=\"javascript:ClientDeleteRelation('" + CmsDataRelations[zIndex].RelationDataId + "','" + OCmsData.frkGroupDataId + "','" + OCmsData.DataId + "','" + RelationTypes[iIndex].RelationTypeId + "');\" src=\"resources/images/delete.png\"> Số: <span class=DocNo>" + CmsDataRelations[zIndex].DataAlias + "</span> - <a href=\"?gid=" + gid + "&id=" + CmsDataRelations[zIndex].DataId + "&back=" + BackAspx + "\">" + CmsDataRelations[zIndex].Subject + "</a>\r\n" +
                                "</li>\r\n";
                        }
                        Html +=
                        "</ul>\r\n" +
                        "</div>\r\n";
                    }
                }
                Html +=
                   " <div class=\"clr\"></div>\r\n" +
                   " </div>" +
                   "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        //[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        //public AjaxOut ServerSideProcessAjaxNextLink(
        //    string SiteId,
        //    string WorkflowDefId,
        //    string WorkflowActionId,
        //    string RecordId)
        //{
        //    AjaxOut OAjaxOut = new AjaxOut();
        //    try
        //    {
        //        WebSessionUtility.CheckSessionTimeOut();
        //        new WorkflowBll().CheckDoNextLinkRunAtServer(SiteId, WorkflowActionId);
        //        string Html = new AjaxWorkflowTaskNextLink().Draw(SiteId, WorkflowDefId, WorkflowActionId, RecordId, LinkId);
        //        OAjaxOut.HtmlContent = Html;
        //    }
        //    catch (Exception ex)
        //    {
        //        OAjaxOut.Error = true;
        //        OAjaxOut.HtmlContent = ex.Message.ToString();
        //        OAjaxOut.InfoMessage = ex.Message.ToString();
        //    }
        //    return OAjaxOut;
        //}

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideStartWorkflow(
            string SiteId,
            string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DataId);
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).SendForPublish(SiteId, DataId);
                OAjaxOut.RetUrl = "?gid=" + OCmsData.frkGroupDataId + "&id=" + OCmsData.DataId + "&back=DocumentInputData.aspx";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAddRelationDocument(
            string SiteId,
            string GroupDataId,
            string SrcDataId,
            string RelationTypeId,
            string documentNo,
            string BackAspx)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, documentNo);
                if (OCmsData == null)
                {
                    throw new Exception("Số văn bản liên quan không hợp lệ! Không tìm thấy số văn bản này!");
                }
                if (OCmsData.Equals(SrcDataId))
                {
                    throw new Exception("Số văn bản liên quan không hợp lệ! Văn bản liên quan không thể chính là văn bản này!");
                }
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).AddDocumentRelation(SiteId, SrcDataId, OCmsData.DataId, RelationTypeId);
                OAjaxOut.RetUrl = "?gid=" + GroupDataId + "&id=" + SrcDataId + "&back=" + BackAspx;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDeleteRelationDocument(
            string SiteId,
            string RelationDataId,
            string GroupDataId,
            string SrcDataId,
            string RelationTypeId,
            string BackAspx)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).DeleteDocumentRelation(SiteId, RelationDataId);
                OAjaxOut.RetUrl = "?gid=" + GroupDataId + "&id=" + SrcDataId + "&back=" + BackAspx;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string ServerSideGetNewDataId(string SiteId)
        {
            return System.Guid.NewGuid().ToString();
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public AjaxOut ServerSideDownloadDocument(string SiteId, string DocumentId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DocumentId);
                if (string.IsNullOrEmpty(OCmsData.AttachDoc)) throw new Exception("Chưa có tài liệu số gắn kèm!");
                OAjaxOut.RetUrl = OCmsData.GetAttachDocUrl(SiteId, WebConfig.HttpDataRoot(OSiteParam));
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public AjaxOut ServerSideSendPublish(string SiteId, string DocumentId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).SendForPublish(SiteId, DocumentId);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAddShortCut(string SiteId, string LinkType, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                UserQuickLinkCls OUserQuickLink = new UserQuickLinkCls();
                OUserQuickLink.frkUserId = UserId;
                OUserQuickLink.LinkType = LinkType;
                OUserQuickLink.SortIndex = 0;
                OUserQuickLink.Subject = "Csdl: " + OGroupData.GroupDataName;
                OUserQuickLink.ObjectId = GroupDataId;
                OUserQuickLink.LinkUrl = "modules/cms/DocumentInputData.aspx?gid=" + GroupDataId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddUserQuickLink(SiteId, OUserQuickLink);
                OAjaxOut.InfoMessage = "Tạo shortcut ra màn hình chính thành công!";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = "Đã có shortcut này không tạo thêm nữa";
                OAjaxOut.HtmlContent = "Đã có shortcut này không tạo thêm nữa";
                ex.Source = "";
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAddShortCutView(string SiteId, string LinkType, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DataId);
                UserQuickLinkCls OUserQuickLink = new UserQuickLinkCls();
                OUserQuickLink.frkUserId = UserId;
                OUserQuickLink.LinkType = LinkType;
                OUserQuickLink.SortIndex = 0;
                OUserQuickLink.Subject = "Văn bản: " + OCmsData.DataAlias + " / " + OCmsData.Subject;
                OUserQuickLink.ObjectId = DataId;
                OUserQuickLink.LinkUrl = "modules/cms/PreviewCmsData.aspx?gid=" + OCmsData.frkGroupDataId + "&CmsDataId=" + OCmsData.DataId + "&back=home.aspx";
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddUserQuickLink(SiteId, OUserQuickLink);
                OAjaxOut.InfoMessage = "Tạo shortcut ra màn hình chính thành công!";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = "Đã có shortcut này không tạo thêm nữa";
                OAjaxOut.HtmlContent = "Đã có shortcut này không tạo thêm nữa";
                ex.Source = "";
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideIndex(string SiteId, string GroupDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ExecuteIndexService(SiteId);
                OAjaxOut.InfoMessage = "Cập nhật index tìm kiếm thành công!";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                ex.Source = "";
            }

            return OAjaxOut;
        }







        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(
            string SiteId,
            string GroupDataId,
            string ParentGroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                GroupDataFolderCls[] GroupDataFolders =
                    CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataFolder(null, SiteId, GroupDataId, ParentGroupDataFolderId);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    Html +=
                            "<li id=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\" class=\"folder expanded\"><a target=\"_self\" href=\"https://localhost:81/public/sbv/admin/modules/cms/AddCmsDocumentData.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolders[iIndex].GroupDataFolderId + " target='_self' \">" + GroupDataFolders[iIndex].FolderName + " </a>\r\n" +
                                BuildSubTreeView(SiteId, GroupDataId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        public static AjaxOut RenderFolder(
            string SiteId,
            RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");

                string gdf = (string)WebEnvironments.Request(SiteId, "gdf");
                if (string.IsNullOrEmpty(gdf)) gdf = "";
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Không tìm thấy nhóm dữ liệu mã " + ORenderInfo.Alias);

                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");

                GroupDataFolderCls[] GroupDataFolders =
                    CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataFolder(null, SiteId, gid, null);
                if (GroupDataFolders.Length == 0)
                {
                    OAjaxOut.HtmlContent = "<input id=txtCheckFolder value=\"0\" type=hidden>";
                    return OAjaxOut;
                }
                string Html =
                    "<input id=txtCheckFolder value=\"1\" type=hidden>\r\n" +
                    "<input id=txtGroupDataId value=\"" + GroupDataId + "\" type=hidden>\r\n" +
                    "<div class=TreeHolder>\r\n" +
                    "<div id=\"tree\" >\r\n" +
                    "<ul>\r\n" +
                    " <li id=\"root\" class=\"folder expanded\"><a href=\"root\" target=\"_self\">" + OGroupData.GroupDataName + "</a></a>\r\n" +
                    "     <ul>\r\n";
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    Html +=
                        "<li class=\"folder expanded\" id=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\"><a href=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\" onclick=\"javascript:ClientAdd('" + GroupDataId + "','" + GroupDataFolders[iIndex].GroupDataFolderId + "')\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                        //"<li class=\"folder expanded\" id=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\"><a href=\"AddCmsDocumentData.aspx?gid=" + GroupDataId + "&GroupDataFolderId=" + GroupDataFolders[iIndex].GroupDataFolderId + " \" target=\"_self\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                                    BuildSubTreeView(SiteId, gid, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n" +
                        "</ul>\r\n" +
                        "</div><br>\r\n";
                //"<script language=javascript>\r\n" +
                //"   function setActiveNode()\r\n"+
                //"   {\r\n"+
                //"       ActiveClick = false;\r\n"+
                //"       $(\"#tree\").dynatree(\"getTree\").activateKey(\"" + gdf + "\");\r\n" +
                //"       ActiveClick = true;\r\n" +
                //"   }\r\n"+
                //"</script>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

    }
}
